iOS drawInRect:attributes 执行多行文本截断 [英] iOS drawInRect:attributes perform text truncation with multiple lines

查看:55
本文介绍了iOS drawInRect:attributes 执行多行文本截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在带有尾部截断的矩形中绘制长文本字符串?我尝试将 drawInRect:withAttributes: 与 NSLineBreakByTruncatingTail 段落样式一起使用,但它始终将文本呈现在一行上.它仅使用 NSLineBreakByWordWrapping 和 NSLineBreakByCharWrapping 中断模式呈现多行文本,并且在使用此选项时没有截断.为了实现这一点,我需要在段落样式上设置任何属性吗?或者这只是不再受支持,在这种情况下,除了继续使用已弃用的方法之外,我看不到其他选择.已弃用的方法 drawInRect:withFont:lineBreakMode:alignment: 工作正常.

How can I draw a long text string in a rect with tail truncation? I tried to use drawInRect:withAttributes: with NSLineBreakByTruncatingTail paragraph style, but it always renders text on a single line. It only renders multi-line text with NSLineBreakByWordWrapping and NSLineBreakByCharWrapping break modes and when using this option there is no truncation. Is there any property that I need to set on the paragraph style in order to make this happen? Or is this simply no longer supported, in which case I can see no alternative other than continuing to use deprecated methods. The deprecated method drawInRect:withFont:lineBreakMode:alignment:worked correctly.

推荐答案

这是可能的.

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

You are right - you can't use NSLineBreakByTruncatingTail paragraph style because it will show only single line of truncated text.

你应该使用 drawWithRect:options:attributes:context:

[string drawWithRect:CGRectMake(x, y, width, height)
             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
          attributes:@{NSFontAttributeName:<font>, NSForegroundColorAttributeName:<color>}
             context:nil];

这篇关于iOS drawInRect:attributes 执行多行文本截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆