简体   繁体   English

获取粗体属性文本的uilabel高度

[英]Getting uilabel height for bold attributed text

My code to calculate height required to for label is as follows: 我计算标签所需高度的代码如下:

 -(float)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:     (float)width{
       NSDictionary *attributesDictionary = [NSDictionary   dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
       CGRect frame = [text boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:      (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary
                                  context:nil];
      // This contains both height and width, but we really care about height.
       return frame.size.height;

  }

I have called it as follows from below code to calculate height firs then used it to draw label 我从下面的代码中如下调用它来计算高度冷杉,然后用它绘制标签

    //form attributed title
    NSString *str_title =@"This is sample title to calculate height";
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setLineSpacing: 2.0f];
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName:@"PTSans-Bold" size:10], NSParagraphStyleAttributeName: paragraphStyle };
    NSAttributedString *attributed_title = [[NSAttributedString alloc] initWithString:str_title attributes:attributes];
    //calculate height required for title
    float comment_height = [self frameForText:str_title sizeWithFont:[UIFont fontWithName:@"PTSans-Bold" size:10] constrainedToSize:250];

    UILabel *lbl_title;
    //use calculated height here
    lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 250, title_height)];
    lbl_title.numberOfLines = 0;
    lbl_title.attributedText = attributed_title;

This works fine when font is "PTSans-Regular" and gives exact uilabel height. 当字体为“ PTSans-Regular”并给出确切的uilabel高度时,此方法可以正常工作。 But, it is not working for "PTSans-Bold" by above code. 但是,上面的代码不适用于“ PTSans-Bold”。

How should I return exact UIlabel needed to write "PTSans-Bold" text with label of width 250, font's size 10 and paragraph line spacing equal to 2? 我应该如何返回编写宽度为250,字体大小为10,段落行间距等于2的“ PTSans-Bold”文本所需的确切UIlabel? Note: the "PTSans-Bold" is not system font, it's font I have added. 注意:“ PTSans-Bold”不是系统字体,而是我添加的字体。

Thanks. 谢谢。

This is easyiest way to find UILabel text height dynamically height for below iOS7 这是在iOS7以下动态查找UILabel文本高度的最iOS7

CGSize fontSize = [uilabel.text sizeWithFont:uilabel.font];
NSLog(@"height %f",fontSize.height);

for iOS7 适用于iOS7

float heightIs =[uilabel.text boundingRectWithSize:uilabel.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:uilabel.font } context:nil].size.height;

use the below method after setting font and every properties. 设置字体和每个属性后,请使用以下方法。

- (CGFloat)getHeight:(UILabel *)label{

      CGSize sizeOfText = [label.text boundingRectWithSize: CGSizeMake( self.bounds.size.width,CGFLOAT_MAX)
                                                  options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                               attributes: [NSDictionary dictionaryWithObject:label.font
                                                forKey:NSFontAttributeName]
                                                  context: nil].size;

    return sizeOfText.height; 

    }

OK, I solved this problem with below code: OK,我用下面的代码解决了这个问题:

-(float)heightOfAttrbuitedText:(NSAttributedString *)attrStr width:(CGFloat )width{

    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return rect.size.height;
}

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

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