Fork me on GitHub

UILabel

http://stackoverflow.com/questions/990221/multiple-lines-of-text-in-uilabel

Multiple lines of text in UILabel

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

it does't work in my case. I'm doing like this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
        //...
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
    return size.height + 20;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
            //...
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Georgia-Bold" size:18.0];
}
        //...
        UILabel * textLabel = [cell textLabel];
        CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0]
                       constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
        cell.textLabel.frame = CGRectMake(0, 0, size.width + 20, size.height + 20);
        //...
    }

 

 

 

 

      The best solution I have found (to an otherwise frustrating problem that should have been solved in the framework) is similar to vaychick's.

Just set number of lines to 0 in either IB or code

myLabel.numberOfLines =0;

This will display the lines needed but will reposition the label so its centered horizontally (so that a 1 line and 3 line label are aligned in their horizontal position). To fix that add:

 

CGRect currentFrame = myLabel.frame;
CGSize max = CGSizeMake(myLabel.frame.size.width, 500)
CGSize expected = [myString sizeWithFont:myLabel.font constrainedToSize:max lineBreakMode:myLabel.lineBreakMode]; 
currentFrame.size.height = expected.height;
myLabel.frame = currentFrame;

 

 

 

 

Problem: Determine the size (number of lines) a UILabel needs, assuming the width is 300 px. The string is longer, so I set the lineBreakMode to UILineBreakModeWordWrap and invoked sizeThatFits to try to determine the size. But it gives a width of 457 px in a single line, rather than the expected 300px in two lines.

Please see:

CGSize available =CGSizeMake(300, INFINITY);
UILabel*label =[[[UILabel alloc] initWithFrame:CGRectMake(0,0,300,400)] autorelease];
label
.text = title;
label
.lineBreakMode =UILineBreakModeWordWrap;
label
.font =[UIFont fontWithName:kBoldFont size:kTitleFontSize];
label
.numberOfLines =3;
CGSize sizedtoFit =[label sizeThatFits:available];

But I find that the sizedtoFit variable has a width of 457 pixels and a height of 22 px, and the UI displays a single line with clipped text. I expect a width of 300 pixels, and a height of 44 px for two lines.

The UILabel doc for numberoflines says:

When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

I tried various combinations of:

  1. Passing CGRectZero to the init function, passing 300x400 or 300 x infinity.
  2. Setting the frame after creation rather than passing it to the init function.
  3. Invoking [sizeToFit] and hoping it calculates the height assuming present width, but it doesn't.
  4. Calling sizeToFit and then calling sizeThatFits.
  5. Invoking layoutIfNeeded.

None of them works. What am I doing wrong, or is this is bad bug where the documentation and the framework implementation don't agree? Thanks.

 
 
 
 

Have you tried the sizeWithFont: constrainedToSize: lineBreakMode: method?

For example:

 

CGSize sizeToFit = [title sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];

 

That was my fallback option, but the question is: why is sizeThatFits not working as it should? I was just told that if I set the numberOfLines to 0 (unlimited), it works. 

This is no good if you want a maximum of three lines, like the question implied. Guess I need to calculate the height of three lines and pass it in to the sizeWithFont function then. Which sucks.

 

 
 
 
I had the same problem, size that fits simply ignores the size... /: I ended up using:
CGRect textSize = [UILabel textRectForBounds:CGRectMake(0, 0, 300, CGFLOAT_MAX) 
                      limitedToNumberOfLines:3];

The documentation says you shouldn't call it directly

 

 

I think u are getting unexpected result because u are not taking into consideration the font of UILabel try following

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)] autorelease];
label.text = title;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:kBoldFont size:kTitleFontSize];
label.numberOfLines = 0;

CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width,FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);

This solution doesn't limit the number of lines 

In addition to not limiting the number of lines, it uses the NSString sizing method, which will ignore a ton of the additional capabilities of UILabel 

 

 
http://blog.csdn.net/huangbaoyu1840/article/details/6712514
UILable显示文字,大多数情况下文字是动态的,所以UILable的大小需要自适用,下面是实现的具体代码:

    myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
                    [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
                    [myLable setNumberOfLines:0];
                    [myLable setBackgroundColor:[UIColor clearColor]];
                    [myAdView addSubview:myLable];

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
       CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
       CGRect rect=myLable.frame;
       rect.size=size;
       [myLable setFrame:rect];
       [myLable setText:text];


- (void)textViewDidChange:(UITextView *)textView{
if(textView.text.length > 20)//一行最多多少字节
{
        //TextView底面背景图片根据内容自动调整高度
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inputbox"ofType:@"png"]];
[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];

UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(320, 140)lineBreakMode:UILineBreakModeWordWrap];
BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
InputTextVeiw.contentInset = UIEdgeInsetsZero;//以换行为基准
[textView setFrame:CGRectMake(51, 210-size.height+18, 200, size.height+5)];
}
}

 

 
 

 

 

textLabel.font =  [UIFont boldSystemFontOfSize:30] ;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.lineBreakMode = UILineBreakModeTailTruncation;
textLabel.adjustsFontSizeToFitWidth = NO;

posted on 2012-04-20 14:22  pengyingh  阅读(1466)  评论(0编辑  收藏  举报

导航