Set distance between two dynamic Label - ios

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;

Related

UILabel sizeThatFits too wide for 1-line label

I have a label that I'm creating and displaying programmatically. It can be 1 or more lines. I want to the label to be truncated at the end if it's too long. When the label is > 1 line long the following code works fine. Create a blank project and drop this into viewDidLoad to play along at home. Any iOS or tvOS project should do.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.backgroundColor = [UIColor blueColor];
[self.view addSubview:label];
NSDictionary *attributes = #{NSFontAttributeName:[UIFont systemFontOfSize:26.0]};
label.attributedText = [[NSAttributedString alloc] initWithString:#"The rain in Spain falls mainly on the plain." attributes:attributes];
CGSize maxLabelSize = CGSizeMake(200, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxLabelSize];
NSLog(#"requiredSize: %#", NSStringFromCGSize(requiredSize));
label.frame = CGRectMake(50.0, 50.0, requiredSize.width, requiredSize.height);
However, if I change numberOfLines to 1 then sizeThatFits returns a size with a width wide enough to fit the entire string even though it's bigger than the width of maxLabelSize.
I can work around this by checking to see if requiredSize.width is greater than maxLabelSize.width, and adjusting appropriately, but I'd like to know why sizeThatFits behaves differently with a 1-line label than with a multi-line label. I would expect a size no greater than 200 with the height the same as the attributed string's line height.
I have no idea why sizeThatFits doesn't work, but another method textRectForBounds:limitedToNumberOfLines: does the trick. Something like
label.numberOfLines = 0;
CGSize requiredSize = [label textRectForBounds:CGRectMake(0, 0, 200, CGFLOAT_MAX) limitedToNumberOfLines:1].size;
UILabel * commlbl;
commlbl=[[UILabel alloc]initWithFrame:CGRectMake(10, commlbl1.bounds.size.height+50, commscroll.bounds.size.width-25, commscroll.bounds.size.height+70)];
[commlbl setFont:[UIFont fontWithName:#"OpenSans-Regular" size:16]];
[commlbl setTextColor:[UIColor whiteColor]];
[commlbl setTextAlignment:NSTextAlignmentCenter];
commlbl.lineBreakMode = NSLineBreakByWordWrapping;
commlbl.numberOfLines = 0;
commlbl.text = [USER_DFT GetUserDefault:#"msgString"];
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [commlbl.text sizeWithFont:commlbl.font constrainedToSize:maximumLabelSize lineBreakMode:commlbl.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = commlbl.frame;
newFrame.size.height = expectedLabelSize.height;
commlbl.frame = newFrame;

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

How add Multiple Lines in Same UILabel

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.

How to have a page with long text that is wrapped automatically based on the phone screen

I am trying to make a book application. I am stucked with the text that can show long string. Basically, I tried to use UI Label, but it is only showing one line and the remaining of the text is cut.
Any idea what kind of UI that I should use to make a book? Thanks.
Set the numberOfLines for the UILabel to 0, to let it use as many lines as it needs.
You have to use UITextView for such purpose check
UITextView *aboutStable = [[UITextView alloc] init];
[aboutStable setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[aboutStable setText:#"Write your long text here............iphonemaclover is great"];
[aboutStable setTextColor:[UIColor grayColor]];
[aboutStable setBackgroundColor:[UIColor clearColor]];
[aboutStable setTextAlignment:UITextAlignmentCenter];
[aboutStable setFrame:CGRectMake(0, 302, 320, 79)];
[self addSubview:aboutStable];
And make it not editable so set
[aboutStable setUserInteractionEnabled:NO]
should do it
and if you need scrolling:
aboutStable.editable = NO;
or if you text is still more,then you can set scrollview in it too...
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = #"your long text here";
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];//add scrollview using Xib and set property of it.
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
or you can check more detail here in this sample code
EDIT FOR DIFFERENT RETINA DISPLAY
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (screenRect.size.height == 568.0f)
{
//condition for iphone 5 screen
}
else
{
//condition for retina display 3.5
}

Resources