iOS drawInRect:属性使用多行执行文本截断

问题描述 投票:4回答:1

如何在带有尾部截断的矩形中绘制长文本字符串?我尝试使用drawInRect:withAttributes:使用NSLineBreakByTruncatingTail段落样式,但它总是在一行上呈现文本。它只使用NSLineBreakByWordWrapping和NSLineBreakByCharWrapping中断模式呈现多行文本,并且在使用此选项时不会发生截断。我是否需要在段落样式上设置任何属性才能实现此目的?或者这是不再支持的,在这种情况下除了继续使用弃用的方法之外我别无其他选择。不推荐使用的方法drawInRect:withFont:lineBreakMode:alignment:工作正常。

ios nsstring uikit
1个回答
2
投票

有可能的。

你是对的 - 你不能使用NSLineBreakByTruncatingTail段落样式,因为它只显示单行的截断文本。

你应该使用drawInRect:withFont:lineBreakMode:alignment:而不是弃用drawWithRect:options:attributes:context:

[string drawWithRect:CGRectMake(x, y, width, height)
             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
          attributes:@{NSFontAttributeName:<font>, NSForegroundColorAttributeName:<color>}
             context:nil];
© www.soinside.com 2019 - 2024. All rights reserved.