ios中根據文本設置label高度

UILabel * label = [[UILabel alloc] init];      web

    NSString * text = @"hellohellohellohellohellohellohellohellohellohellohellohello ";      app

    label.text = text;  字體

    [label setNumberOfLines:0];  spa

    UIFont *font = [UIFont fontWithName:@"Arial" size:14];      orm

    //設置字體      ip

    label.font = font;      it

    CGSize size = CGSizeMake(300, 20000.0f);  io

    size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];  import

    [label setFrame:CGRectMake(10, 0, size.width, size.height)];  webkit

   [self.view addSubview:label];  

lineBreakMode:設置標籤文字過長時的顯示方式。 

label.lineBreakMode = NSLineBreakByCharWrapping;以字符爲顯示單位顯示,後面部分省略不顯示。 

label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內容長度,後半部分被刪除。 

label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,顯示尾部文字內容。 

label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內容以……方式省略,顯示頭尾的文字內容。 

label.lineBreakMode = NSLineBreakByTruncatingTail;結尾部分的內容以……方式省略,顯示頭的文字內容。 

label.lineBreakMode = NSLineBreakByWordWrapping;以單詞爲顯示單位顯示,後面部分省略不顯示。

UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]};
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributes
                                          context:nil];

expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height);

相關文章
相關標籤/搜索