简体   繁体   English

iOS更改标签的数字行

[英]iOS change number lines of label

I tried to code the number of lines for one label. 我试图编码一个标签的行数。

I did this : 我是这样做的:

[lblName setFont:[UIFont fontWithName:@"OpenSans-CondensedLight" size:19]];
[lblName setText:[objet titre]];
[lblName setLineBreakMode:NSLineBreakByWordWrapping];
lblName.numberOfLines = 2;

But it's not running, i've just one line... 但是它没有运行,我只有一行...

Someone to help me plz ? 有人可以帮助我吗?

For set dynamic frame UILabel use following method 对于设置动态框架UILabel使用以下方法

-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
    CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
    CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
    CGRect lblFrame = myLabel.frame;
    lblFrame.size.height = expecteingmyLabelSize.height;
    myLabel.frame = lblFrame;
    int addressLine = myLabel.frame.size.height/fontSize;
    myLabel.numberOfLines = addressLine;
}

In the above method you just need to pass your label object, width of your label and font size of text, such like... 在上述方法中,您只需要传递标签对象,标签宽度和文本的字体大小,例如...

[self setDynamicHeightOfLabel:lblName withLblWidth:passWidth andFontSize:19];

Use this method: 使用此方法:

- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
    CGSize constraintSize;
    constraintSize.height = MAXFLOAT;
    constraintSize.width = width;
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
                                          nil];

    CGRect frame = [text boundingRectWithSize:constraintSize
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:attributesDictionary
                                      context:nil];

    CGSize stringSize = frame.size;
    return stringSize;
}

Set the CGSize returned from here as your label frame size. 将从此处返回的CGSize设置为label帧大小。

You need to increase the height of UILabel. 您需要增加UILabel的高度。 Just use following method to get the height of uilabel text and then set number of lines as following: 只需使用以下方法获取uilabel文本的高度,然后按如下所示设置行数:

- (CGSize)calculateRowHeightAndWidth:(NSString *)text font:(UIFont *)font width:(CGFloat)width
{
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 9999)
                       lineBreakMode:UILineBreakModeWordWrap];
    return size;
}

    textDetailLabel.numberOfLines = size.height/height size of font.0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM