
[英]Find width of attributed text in a UILabel when the text has multiple lines
[英]UILabel attributed text can't have multiple lines if you change baseline offset attribute
我想用一个词来更改括号的基线偏移,例如“ [推] blablabla”。
NSRange range = [text rangeOfString:@"[推]"];
if (range.location == 0) {
[text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(0, 1)];
[text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(range.length-1, 1)];
}
但标签的第二行消失并被截断。 有人有主意吗?
请同时使用lineBreakMode和numberOfLines来调用sizeToFit,如下所示:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit];
高度将自动计算。
谢谢
我进行了一些快速测试,似乎如果在字符串的开头添加空格,它将按预期工作
// make sure your text looks like @" [推]your content"
if (range.location == 1) {
[text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(1, 1)];
[text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(range.length, 1)];
}
您是否尝试使用NSMutableParagraphStyle
将lineBreakMode设置为属性?
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [attributedText length])];
像这里建议的那样: 具有多个换行模式的UILabel attributedText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.