1

I am getting JSON response from server and set that contents to UILabel and sometimes the text will be one line and sometimes the text will be 10 lines.So i display this UILabel inside the UIScrollView. But i got the empty space in UILabel

This is my screenshots..

enter image description here

enter image description here

Currently i am using storyboard and auto layout

So i want to avoid these empty space in UILabel. Please give me suggestions for this alignment issues.??

Code for changing UILabel height but this is not working for me

CGRect textRect = [strDetailsFinal boundingRectWithSize:CGSizeMake(296, FLT_MAX)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName: labDetails.font}
                                                context:nil];

CGSize size = textRect.size;
CGRect newFrame = labDetails.frame;
newFrame.size.height = size.height;
labDetails.frame = CGRectMake(160, 200, 300, newFrame.size.height);
Hiren
  • 12,720
  • 7
  • 52
  • 72
Kalai
  • 469
  • 2
  • 9
  • 20
  • yes beter you calculate the height or width of text and then set the frame of label it should be based on text of Json and by this you will achive you task – kamalesh kumar yadav Jan 31 '14 at 04:43
  • @kamaleshkumaryadav yes i just edited my question and posted a lines of codings for changing uilabel height dynamically.But this is not work for me :( – Kalai Jan 31 '14 at 04:54
  • @Kalai - Remember one thumb rule - You should not set frame when using autolayout - You are going against Autolayout basic concept - Instead try to autolayout constraints programatically in your case – bhavya kothari Jan 31 '14 at 05:06
  • 1
    @bhavyakothari ok give some sample codings for setting frame to uilabel when auto layout is ON – Kalai Jan 31 '14 at 05:17

6 Answers6

2

May this help you

NSDictionary *attributesDic= [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIFont fontWithName:@"FontName" size:15], NSFontAttributeName,nil];

CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0)
                                                 options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDic context:nil];

CGSize size = frame.size;

and its possible answer Dynamically resize label in iOS 7 or you can try this https://gist.github.com/danielphillips/1005520

Community
  • 1
  • 1
  • Your coding is only applicable for auto layout disable..But currently i am using auto layout. – Kalai Jan 31 '14 at 05:03
2

Use [labDetails sizeToFit]. If you have size constraints then use [sizeThatFits:CGSizeMake(WidthConstraint, HeightConstraint)] then use the result of that to set your label frame.

Infinity
  • 3,695
  • 2
  • 27
  • 35
2

why don't you use UIButton instead of UILabel. UIButton has property to align the text vertically, while UILabel doesn't.

Use UIButton with userInteractionEnabled = NO. Check my this reply here: Vertically align UILabel

Community
  • 1
  • 1
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
2

Remember one thumb rule - You should not set frame when using autolayout - You are going against Autolayout basic concept - Instead try to autolayout constraints programatically in your case

-(void)awakeFromNib{

[super awakeFromNib];
for(NSLayoutConstraint *cellConstraint in self.constraints){
    [self removeConstraint:cellConstraint];
    id firstItem = cellConstraint.firstItem == self ? self.contentView : cellConstraint.firstItem;
    id seccondItem = cellConstraint.secondItem == self ? self.contentView : cellConstraint.secondItem;
    NSLayoutConstraint* contentViewConstraint =
    [NSLayoutConstraint constraintWithItem:firstItem
                             attribute:cellConstraint.firstAttribute
                             relatedBy:cellConstraint.relation
                                toItem:seccondItem
                             attribute:cellConstraint.secondAttribute
                            multiplier:cellConstraint.multiplier
                              constant:cellConstraint.constant];
    [self.contentView addConstraint:contentViewConstraint];
}
}
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
2

You have taken a fix size UILable rather you have to use a dynamic size UILable.

Before that you need to calculate the size of text/string

 #define FONT_SIZE 15.0f
 #define LABEL_CONTENT_WIDTH 320.0f
 #define LABEL_CONTENT_MARGIN 10.0f

NSString *text = @"YOUR TEXT HERE";
CGSize constraint = CGSizeMake(LABEL_CONTENT_WIDTH - (LABEL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);

after calculating the size, apply them to your label like below

[labDetails setFrame:CGRectMake(labDetails.frame.origin.x, labDetails.frame.origin.y, labDetails.frame.size.width, MAX(height, 44.0f))];

And If you are using autolayout option then please check here: Link

Community
  • 1
  • 1
Hiren
  • 12,720
  • 7
  • 52
  • 72
1

Use Upper label frame's origin and size, and based on that set your detail labels frame, Use sizeToFit property of label.

Suppose 582,618 is your title label,

Like,

labDetails.frame=CGRectMake(5, title.frame.origin.x+title.frame.size.height+5, 300, 100);
[labDetails sizeToFit];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121