菜鸟一个,谢谢指教。
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:item.content];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:24];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [item.content length])];
[label setAttributedText:attributedString1];
CGSize boundSize = CGSizeMake(IPHONE_WIDTH-32, CGFLOAT_MAX);
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : paragraphStyle1};
CGSize size = [item.content boundingRectWithSize:boundSize options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
return size.height;
这是代码
根据这个方法来实现
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
解释一下:
第一个参数size:这个是一个CGSize格式的,宽度是cell的宽 或者你定义的任何控件或者什么东西。。。高度取CGFLOAT_MAX表示无限大
第二个参数options是一个(NSStringDrawingOptions)类型的,是一个枚举值
typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin
NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
} NS_ENUM_AVAILABLE(10_0, 6_0);
NSStringDrawingUsesLineFragmentOrigin:
The specified origin is the line fragment origin, not the base line origin
指定的orgin是line frgment orgin,不是base line origin.英文不好。。
NSStringDrawingUsesFontLeading:
使用字体间的行距来计算。行距=字体大小+间距
NSStringDrawingUsesDeviceMetrics:
我理解的就是计算的时候加上特殊字符比如/r/n/t这些东西
NSStringDrawingTruncatesLastVisibleLine:
[如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。]
attributes参数传入的是一个字典:参数格式如下
NSDictionary
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:item.content];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:24];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [item.content length])];
这是创建了一个attributedString1的NSMutableAttributedString对象
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
创建一个装饰字体的对象 给他设置行间距
[paragraphStyle1 setLineSpacing:24];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [item.content length])];
设置添加的字体长度
今天我碰到的问题 上面那些都写了以后
要设置下面这句,网上大部分写的都是
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:12]};
这一句加上了设置的paragraphStyle,这样最后得到的CGSize是所有字体按照目标的size算出来的,我需要拿到这个高度。。。。
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : paragraphStyle};
文笔不好。。。写了半天。。。。