iOS drawInRect:attributes perform text truncation with multiple lines

帅比萌擦擦* 提交于 2020-01-01 12:36:09

问题


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.


回答1:


It is possible.

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

Instead of deprecated drawInRect:withFont:lineBreakMode:alignment: you should use drawWithRect:options:attributes:context:

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


来源:https://stackoverflow.com/questions/31836385/ios-drawinrectattributes-perform-text-truncation-with-multiple-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!