1.font的size是个大小 可以乘以系数和一个因数
2.宽度固定 label.adjustsFontSizeToFitWidth = YES; 或者XIb勾选
3.强制竖间距
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.maximumLineHeight = lineH;
paragraphStyle.minimumLineHeight = lineH;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
CGFloat baselineOffset = (lineH - _explainLabel.font.lineHeight) /4;
[attributes setObject:@(baselineOffset)forKey:NSBaselineOffsetAttributeName];
UILabel*label = [[UILabelalloc]init];
label.attributedText = [[NSAttributedString alloc] initWithString:_explainLabel.text attributes:attributes];
4.横间距 https://blog.csdn.net/minggeqingchun/article/details/77334147
* 获取富文本的高度
*
* @param string 文字
* @param lineSpace 行间距
* @param kern 字间距
* @param font 字体大小
* @param width 文本宽度
*
* @return size
*/
- (CGSize)getAttributionHeightWithString:(NSString*)string lineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont*)font width:(CGFloat)width {
NSMutableParagraphStyle*paragraphStyle = [NSMutableParagraphStylenew];
paragraphStyle.lineSpacing = lineSpace;
NSDictionary*attriDict = @{
NSParagraphStyleAttributeName:paragraphStyle,
NSKernAttributeName:@(kern),
NSFontAttributeName:font};
CGSizesize = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:attriDict context:nil].size;
returnsize;
}
网友评论