How add Multiple Lines in Same UILabel - ios

Any way to have multiple lines of text in UILabel ?
I dont wish to more than 1 label in the view.
How to add multiple lines in a single UILabel??

Yes there is a way. Just you need to add two property of UILabel i.e.
NumberOfLines=0 It'll allow you to add multiple lines in a UILabel
LineBreakMode = NSLineBreakByWordWrapping It'll allow you to break your sentence by word. You can also change it according to your requirement.
[YourLabel setNumberOfLines:0];
[YourLabel setLineBreakMode:NSLineBreakByWordWrapping];
You can also set this two property form your interface builder

here is a sample code
UILabel *pHolder1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 245, 45)];
pHolder1.backgroundColor = [UIColor clearColor];
pHolder1.font = [UIFont systemFontOfSize:14.0f];
pHolder1.numberOfLines =0;
pHolder1.lineBreakMode = NSLineBreakByWordWrapping;
pHolder1.textAlignment = NSTextAlignmentCenter;
Dynamically calculate the height of UILabel
please refer the below post
Adjust UILabel height depending on the text

Here is sample code:
UILabel *lblUsername=[[UILabel alloc] init];
StoryTextSize = [storytext sizeWithFont:[UIFont fontWithName:#"Georgia" size:13.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
lblUsername.frame=CGRectMake(20, 5, [[UIScreen mainScreen] bounds].size.width-40, StoryTextSize.height);
lblUsername.textColor=[UIColor blackColor];
lblUsername.text=[NSString stringWithFormat:#"%#",[[tblRecords objectAtIndex:indexPath.row] valueForKey:#"username"]];
lblStoryText.numberOfLines=nooflines;
lblStoryText.backgroundColor=[UIColor clearColor];
[self.view addSubview:lblStoryText];
make sure that your label height should be more so total no of lines become visible.

Related

Set distance between two dynamic Label

I want to fix a constant vertical distance between 2 UILabels.
When the first UILabel resizes, the UILabel below should dynamically adjust it's Y position.
This is what I have right now:
CGRect labelFrame = CGRectMake(20, 20, 200, 200);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setBackgroundColor:[UIColor orangeColor]];
NSString *labelText = #"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments...";
[myLabel setText:labelText];
[myLabel setNumberOfLines:0];
[myLabel setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
CGRect labelsrame = CGRectMake(60, 200, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
[myLabel2 setBackgroundColor:[UIColor greenColor]];
NSString *label2Text = #"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments... almost home free, keep jogging, brisk... pace... hrm...";
[myLabel2 setText:label2Text];
[myLabel2 setNumberOfLines:0];
[myLabel2 setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel2 sizeToFit];
[self.view addSubview:myLabel2];
Thanks
[myLabel sizeToFit] (in your case) changes the height parameter of myLabel while myLabel2 still starts at y=200 when it should start (as per your requirement) immediately after myLabel.
Hence, you need to make your frame settings dynamic too.
To fix the Y position issues for myLabel2, the following should be sufficient:
//...
CGRect labelsrame = CGRectMake(60, myLabel.frame.origin.y + myLabel.frame.size.height, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
//...
you have to create dynamic frame for both the label like bellow
UILabel *lblText1 = [[UILabel alloc]initWithFrame:CGRectMake(19, 250, 283, 20)];
lblText1.text = #"What ever your dynamic text";
CGSize maximumLabelSize = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize = [lblText1.text sizeWithFont:lblText1.font
constrainedToSize:maximumLabelSize
lineBreakMode:lblText1.lineBreakMode];
CGRect newFrame = lblText1.frame;
newFrame.size.height = expectedLabelSize.height;
lblText1.frame = newFrame;
// label 2
UILabel *lblText2 = [[UILabel alloc]initWithFrame:CGRectMake(19, 280, 283, 20)];
lblText2.text = #"What ever your dynamic text label 2";
CGSize maximumLabelSize1 = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize1 = [lblText2.text sizeWithFont:lblText2.font
constrainedToSize:maximumLabelSize1
lineBreakMode:lblText2.lineBreakMode];
CGRect newFrame1 = lblText2.frame;
newFrame1.size.height = expectedLabelSize1.height;
newFrame1.origin.y = lblText1.frame.origin.y + lblText1.frame.size.height + 10; // 10 for fix space between 2 label
lblText2.frame = newFrame1;

iOS sizeToFit not showing text

I'm creating these labels for a Storyboard view. I have autolayout set to OFF and I'm setting the numberoflines to 0 but the text is only displayed if I comment out sizeToFit. Then, of course, the text gets cut off when its height is greater than 40.
- (void)viewDidLoad
{
[super viewDidLoad];
first = #"This text fits in the label";
second = #"This large text is too large for the label but because the words are normal sized, shows the more button correctly at the bottom right";
third = #"Doesn't show the more button correctly because WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";
firstlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 57, 295, 40)];
secondlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 152, 295, 40)];
thirdlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 225, 295, 40)];
[firstlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[secondlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[thirdlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
firstlbl.lineBreakMode = NSLineBreakByWordWrapping;
secondlbl.lineBreakMode = NSLineBreakByWordWrapping;
thirdlbl.lineBreakMode = NSLineBreakByWordWrapping;
firstlbl.numberOfLines = 0;
secondlbl.numberOfLines = 0;
thirdlbl.numberOfLines = 0;
[firstlbl sizeToFit];
[secondlbl sizeToFit];
[thirdlbl sizeToFit];
[firstlbl setText:first];
[secondlbl setText:second];
[thirdlbl setText:third];
[self.view addSubview:firstlbl];
[self.view addSubview:secondlbl];
[self.view addSubview:thirdlbl];
}
You're calling sizeToFit before you set the text, so it's sizing down to CGRectZero. Switch those calls and it'll work.
sizeToFit doesn't mean "automatically adjust your size to fit your content," it means "update your size right now to fit your content." Subsequent updates to the text won't change the frame.
If you want the former behavior, so that it automatically sizes itself to its content, autolayout is probably the easiest road to take.

Adjust height according to number of lines

I am trying to adjust height of my view according to how many lines inserted text will take. Then [title numberOfLines] always returns 0 instead of the new number of lines. Is there a way to get current number of lines?
UILabel *title = [[ UILabel alloc] initWithFrame:CGRectMake(0, 80, 320, 50)];
[title setNumberOfLines:0];
float height = 32 * [title numberOfLines];
[self setFrame:CGRectMake(0, yCoord, 320, height)];
I think your problem come from the fact you did n't set any text and font to your UILabel.
What you could use is [UIView sizeToFit]
Here is a short sample code:
UILabel *title = [[ UILabel alloc] initWithFrame:CGRectMake(0, 80, 320, 50)];
[title setText:#"An awesome title very long or something really important like a Lorem Ipsum"]; // a text to base the needed numbers of line on
[title setFont:[UIFont systemFontOfSize:18]]; // don't forget the font
[title setNumberOfLines:0];
[title sizeToFit]; // now the label will be resize to display all lines
[title setFrame:CGRectMake(0, 80, 320, title.frame.size.height)]; // reset the width in case the sizeToFit method resized it wrong.
Ended up using
[title sizeToFit];
int numLines = (int)(title.frame.size.height/title.font.leading);
From another answer

Positioning UILabel inside a UITextView

Trying to position a UILabel above some text, but the elements are "stepping" on each other. I positioned them with x,y,h,w such that they should not do this but alas, they are. You can see in the screenshot that the label text "Abalone" and the text are commingled. What changes do I need to make here? I've already tried changing the x,y,w,h values to no great effect...
txtView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,280,300)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UILabel *wordTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
wordTitle.text = word.term;
[txtView addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
I think the problem is you are adding the label view as a subview to UITextview. You have to reserve some space for the label view in the UITextview. You can either add some padding to the textView, which can be confusing, because padding in UITextView is not constant and varies based on the font size. Or you can add your UILabel directly to the detailsViewController.view and start your UITextView below the label.
Hope it helps.

Get the numberOfLines from a UILabel after resizing it

I do this to make my UIlabel, setting the numberOfLines to 0 so that it has no line-limit:
UILabel *nmLbl = [[UILabel alloc] initWithFrame:CGRectZero];
[nmLbl setFont:[UIFont boldSystemFontOfSize:16.0f]];
[nmLbl setNumberOfLines:0];
[self addSubview:nmLbl];
[nmLbl release];
Later on, when I know which string goes into the label, I size it like this:
nameSize = [[self name] sizeWithFont:[UIFont boldSystemFontOfSize:16.0f] constrainedToSize:maxNameSize lineBreakMode:UILineBreakModeWordWrap];
[self.nameLabel setFrame:CGRectMake(0.0f, 0.0f, nameSize.width, nameSize.height)];
[self.nameLabel sizeToFit];
Now, for my particular use, I need to know how many lines this ends up taking.
If I access the numberOfLines property of the UILabel it will always return 0.
Is there a way for me to directly access how many lines the UILabel ended up being without
having to calculate it, again, by going label.size.height / fontHeight?
Thank you in advance.
No. numberOfLines is a configuration setting, not a reflection of the current formatting.

Resources