【发布时间】:2014-04-16 02:35:42
【问题描述】:
UILabel 中的行间距有问题,我使用的是自定义字体,当我使用表情符号时,两行之间没有空格。这显然看起来不太好。所以我将此代码用于行间距,但应用程序崩溃给出错误
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“NSAttributedString 对自动调整大小无效,它必须具有单个跨段样式(或没有)和非换行 lineBreakMode。”
if ([cell.label2 respondsToSelector:@selector(setAttributedText:)])
{
UIFont *font =btMyriadProRegularWithSize14Pt;
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 22];
NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:chatMessage.msgString attributes:attributes];
[cell.label2 setAttributedText: attributedString];
}
else
{
NSString * msg = [NSString stringWithFormat:@"%@: %@",chatMessage.from,chatMessage.msgString];
cell.label2.text = msg;
}
【问题讨论】:
-
使用 [yourLabel setAdjustsFontSizeToFitWidth:NO];
-
解决的问题是您只需将标签的文本大小自动缩小为固定字体,然后它就可以工作了
-
@NitinGohel 有了这个,它的应用程序不会崩溃,但它只显示一行
-
更改标签。 numberOfLines = 0;
-
有什么方法可以将它与 sizeToFit 一起使用吗?在其他情况下,它没有设置标签的宽度,我需要这样做。