UILabel高度计算

2018-08-21  本文已影响7人  c77f4a115d4f

1.单行文本高度计算

define VPTEXTSIZE(text, font) [text length] > 0 ? [text \

sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero

2.默认不设置行间距时,单行多行都可以

}
2.设置行间距的情况
多行的情况
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 12;
NSDictionary *dic = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:14]};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text attributes:dic];
text4.attributedText = attrString;

height=[text heightWithStringAttribute:dic1 fixedWidth:[UIScreen mainScreen].bounds.size.width-20*2];
if ((height - [UIFont systemFontOfSize:14].lineHeight) <= paragraphStyle.lineSpacing) {//是否是单行
if ([self containChinese:text]) { //如果包含中文
height = height-paragraphStyle.lineSpacing;
// height = [NSString aLineOfTextHeightWithStringAttribute:dic1];

    }
    tex5.text=text;  //单行的情况就不能设置为attributedText了
}else{
    tex5.attributedText = attrString;

}
上一篇 下一篇

猜你喜欢

热点阅读