Muzamil Hassan
Muzamil Hassan

Reputation: 851

dynamic height uilabel

I am trying to change the uilabel height dynamically as the text width changes, but a problem arises when I try to use this line "white wallington hall". Basically it calculates width which is in 2 lines in the uilabel, but when it shows in two lines it look like this:

"white   
 wallington h..."

Sample code:

 UILabel* lbl_Property = [[UILabel alloc]init];
   [lbl_Property setFont:[UIFont fontWithName:@"Arial" size:10]];
   [lbl_Property setNumberOfLines:0];
   [lbl_Property setText:[arr_SubDetail objectAtIndex:x]];
   UIFont* font = [UIFont fontWithName:@"Arial" size:10];
   CGSize stringSize = [lbl_Property.text sizeWithFont:font];
   CGFloat width = stringSize.width;


   [lbl_Property setFrame:CGRectMake(lbl_Property.frame.origin.x, lbl_Property.frame.origin.y, lbl_Property.frame.size.width,5+lbl_Property.frame.size.height*(ceilf(width/110)))];

Actually after "white" there is a space and "wallington" isn't in that line so it goes on a new line, but it is not showing the complete text

How do I get it to show right?

Upvotes: 1

Views: 2643

Answers (4)

Rock
Rock

Reputation: 64

func makeSizeSender(name:UILabel){

    let attributes = [NSFontAttributeName : name.font]
    let rect = name.text!.boundingRectWithSize(CGSizeMake(frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)
    name.frame = CGRectMake(contentView.frame.origin.x+30, 0, 200, rect.height+30);
    self.addSubview(name)

}

Upvotes: 0

Chirag Pipaliya
Chirag Pipaliya

Reputation: 1281

Try it....

//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                    constrainedToSize:maximumLabelSize 
                    lineBreakMode:yourLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

Upvotes: 5

Exploring
Exploring

Reputation: 935

Hope the below code may work

CGSize size = [str sizeWithFont:lbl.font constrainedToSize:CGSizeMake(204.0, 10000.0) lineBreakMode:lbl.lineBreakMode];

here width or height one must be fixed..

Upvotes: 1

Manu
Manu

Reputation: 4750

 int h=10;    
 UILabel *lbl_headitem = [[UILabel alloc]initWithFrame:CGRectMake(3,h, 690, size_txt_overview1.height)];// See the 'h' value
 lbl_headitem.text = [headItemArray objectAtIndex:k];
 [lbl_headitem setTextAlignment:UITextAlignmentLeft];
 [lbl_headitem setBackgroundColor:[UIColor clearColor]];
 [lbl_headitem setTag:k];
 lbl_headitem.numberOfLines=0;  
 [lbl_headitem setTextColor:[UIColor redColor]];
 [lbl_headitem setFont:[UIFont fontWithName:@"Helvetica" size:18]];
 [scrollAttributes addSubview:lbl_headitem];
 h = h + size_txt_overview1.height;

Make sure your label.numberOfLines are set to 0

Upvotes: 3

Related Questions