Text is not getting placed as mentioned in CGRect frame of UILable - iphone

this might look simple but i am not able to Find the solution for this...when i use this below code the text get placed at right postion as mentioned in the CGRect frame.
[self.view addSubview:[self createLabel:CGRectMake(50,480,50,20):#"Status:"]];
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.textAlignment = UITextAlignmentLeft;
}
But when i use this below Code the text is getting placed at rightmost corner of the frame...its not getting placed at right position as mentioned in the CGRect frame..can anyone tell whats the problem happening with this..
[self.view addSubview:[self createLabel:CGRectMake(50,340,150,20):aRequests.request_details]];
NOTE:aRequests.request_details is coming from webservices..

Your method doesn't return any object. Try to add return myLabel; at the end of it.
And you are not setting new title: myLabel.text = labelTitle;

Related

Placing an image in the bottom of an UITextfield as background

After a lot of search I am posting this Question, I have to place an image at the bottom of a UITextfield, I have tried it with the following code :
letterField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
letterField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
letterField.textAlignment = UITextAlignmentCenter;
letterField.borderStyle = UITextBorderStyleNone;
UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"text_line.png"]];
[letterField setLeftView:myView];
[letterField setRightViewMode:UITextFieldViewModeAlways];
[myView release];
// letterField.background = [UIImage imageNamed:#"text_line.png"];
// [letterField setBackground:[UIImage imageNamed:#"text_line.png"]];
letterField.textColor = [UIColor whiteColor];
letterField.returnKeyType = UIReturnKeyDone;
[yourtextfield insertSubview:imgViewTitleBackGround atIndex:0];// mgViewTitleBackGround is image view with background image.
//Or
[yourtextfield setBackground:[UIImage imageNamed:#"text_line.png"]];// you can add your image
yourtextfield.textColor = [UIColor redColor];
Hope, this will help you..enjoy
Sorry, my previous answer was totally wrong. From what I can see your image is text_line.png - i suppose it's some kind of separator line, what can i suggest you to try is create a new UIView with the bounds of the UITextField, then add the text field inside this view and add the UIImage view again as subview of this view centering it properly. I don't know if this will fit your needs, but you can give it a try.

UITextView, user scrolls uitextview but then it snaps back to original position, whats wrong?

I currently have a UITextView which is contained in a UIViewController using the following code:
UIViewController *container = [[UIViewController alloc] init];
container.view.frame = CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width, 1000);
//[UIScreen mainScreen].bounds.size.height
container.view.userInteractionEnabled = YES;
container.view.clipsToBounds = YES;
UIImageView *imgView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"myImage.png"]];
imgView.frame = container.view.frame;
imgView.userInteractionEnabled = YES;
[container.view addSubview:imgView];
[imgView release];
UITextView *textContained = [[UITextView alloc]
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 1000)];
//container.view.bounds.size.height
textContained.font = [UIFont fontWithName:#"Calibri" size:14];
textContained.scrollEnabled = YES;
textContained.editable = NO;
textContained.textColor = [UIColor whiteColor];
textContained.backgroundColor = [UIColor clearColor];
textContained.font = [UIFont boldSystemFontOfSize:14];
textContained.userInteractionEnabled = YES;
textContained.alwaysBounceVertical = YES;
textContained.contentSize = CGSizeMake(container.view.frame.size.width,
container.view.frame.size.height);
I then set my UItextView text property with some text, which extends past the current screen size. I then add my UITextView to my container using the following code.
switch (indexPath.row) {
case 0:
textContained.text = #"LOTS AND LOTS OF TEXT";
[container.view addSubview:textContained];
[self.navigationController pushViewController:container animated:YES];
[textContained release];
break;
default:
break;
}
[container release];
When I test this code, the text appears just fine in the UITextView and everything looks ok. But the problem is when I try to scroll down to see the remainder of the text. Everytime I scroll down the UITextView scrolls back in to its original position. I have tried several ways to get this to work, but I think I need some fresh eyes to see what I'm doing wrong.
Any help is greatly appreciated.
First, you don't want to set the contentSize of a UITextView, since it is determined by the length of text automatically.
Try to make the height of textView smaller. For example:
UITextView *textContained = [[UITextView alloc]
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 50)];
And give it more text, like:
textContained.text = #"LOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n";

Remove label from scrollview

I am creating an iPhone app in which i am using scrollview and adding labels like this :
question = [[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] ;
question.textColor = [UIColor blueColor];
question.text = [NSString stringWithFormat:#"%#" ,selected];
question.lineBreakMode = UILineBreakModeWordWrap;
[question setFont:[UIFont fontWithName:#"Futura" size:30]];
question.backgroundColor = [UIColor clearColor];
question.numberOfLines = 0;
[question sizeToFit];
[self.view addSubview:question];
[scrollview addSubview:question];
now i want to remove this label from scrollview. So how can i do this..??
i am doing this for remove object from main view.
[question removeFromSuperview];
Thanks.
There are some problems in your code. I assume that the scrollview is a subview of self.view. In this case remove the line
[self.view addSubview: question];
from you code. Depending on the rest of your code I would eventually also change the first line. If you don't need to excess the label somewhere else in your code I would change the first line to
UILabel *question = [[UILabel alloc] initWithFrame: CGRectMake(22, 130, 725, 160)];
and add a line after [scrollview addSubview: question]; with
[question release];
This would reduce you memory consumption.
Why are you adding question to the main view and to the scroll view? It doesn't make sense. Remove the [self.view addSubview:question]; line and [question removeFromSuperview]; will remove your label from the scrollview.

iphone : Label values over write.

I am inserting label in UIButton using addSubView
Here the label's value will be change every time.
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"]; // UserIndex will be change and new data will be load in the label
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
The Problem is : It successfully changes the label value but it is over writing it.
I am getting the new label value over the old label value.
How can I solve this ?
Every time you call [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)] you are creating new memory, not accessing the existing memory.
So, you need to have something like
if(!backTopLabel)
backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
so that you only create the memory if it doesn't exist.
However since your label isn't a property, you're essentially losing access to it anyway.
So you either need to add the label as a property of the class you're in OR tag the view so you can find it again & remove it.
Tag it & find again like this:
for(UIView* labelView in btnBackLeftCard.subviews)
{
if(labelView.tag = 100)
[labelView removeFromSuperView];
}
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
//here is where you tag the view, so you can find it again
backTopLabel.view.tag = 100;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"];
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
Assuming you have only one subview to be inserted in UIButton you can use
for(UILabel *lblViews in [btn subviews]) //Remove all subviews which are labels under button first if any
{
if(lblViews.tag == sometagno) //Compare tag number and remove only if that label found.
[lblViews removeFromSuperview];
}
backTopLabel.tag = sometagno; //Assign some tag number while adding.
[btnBackLeftCard addSubview:backTopLabel]; //Than Add your subview
This basically happens when a view gets added as subview on another view.. In your case it's been added multiple times.
Create one UILabel and then simple change it's text property.
In your case you're creating it multiple times and adding on top of the previous one.
Or if you cannot reuse previously created one, try removing it from the superview then add the latest one..
I think you are adding the subview (UILabel) again.
YOu should only change the text of UILabel
I am working in COCOS2D. If you have declared labelname globally then use
[labelName removeFromParentAndCleanup:YES]

Can anyone show me an example on how to add text to a UIScrollView?

Most of the code examples I have seen show images that are added. I am looking for an example that essentially just shows about 10-15 row of data being added to a UIScrollVIew.
Any solid advice would be highly appreciated.
Put this into: viewDidLoad{}
//create UITextView
UITextView *myUITextView = [[UITextView alloc] initWithFrame:CGRectMake(20,188,280,260)];
myUITextView.text = infoText;
myUITextView.textColor = [UIColor lightGrayColor];
myUITextView.font = [UIFont systemFontOfSize:14];
[myUITextView setBackgroundColor:[UIColor clearColor]];
myUITextView.editable = NO;
myUITextView.scrollEnabled = YES;
[Scrollerview addSubview:myUITextView];
[myUITextView release];
where 'infoText' is your text, and 'Scrollerview' is your uiScrollView
Check out UITextView, its fairly easy to implement, but it might not have the versatility that you want.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextView_Class/Reference/UITextView.html