2018.04.18
UILabel控制
- 限制文字2行内显示
- 根据给定的宽度和单行高度,自动展示文字,多余的文字...
- 用段落style,设置行高,lineBreakMode用NSLineBreakByTruncatingTail
self.title.numberOfLines = 2;
self.title.text = title;
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.alignment = NSTextAlignmentLeft;
style.lineSpacing = 5;
// 设置行距
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kCellTitleFontSize], NSParagraphStyleAttributeName:style}];
self.title.attributedText = attributedString;
[self.title sizeToFit];
- 计算UILabel宽高
CGSize titleSize = [self calc:title left:left];
self.title.frame = CGRectMake(left, 0, titleSize.width, titleSize.height);
- 这里的sylte.lineBreakMode要用NSLineBreakByWordWrapping
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineBreakMode = NSLineBreakByWordWrapping;
style.alignment = NSTextAlignmentLeft;
style.lineSpacing = 5;
CGSize maxSize = CGSizeMake(maxWidth, lineHeight * lineNum);
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kCellTitleFontSize], NSParagraphStyleAttributeName:style};
CGSize labelSize = [str boundingRectWithSize:maxSize options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;