简体   繁体   中英

How do I automatically truncate a UILabel on word boundaries with ellipsis?

我有一个1行标签,要在单词边界上截断并添加省略号(即“ The quick brown fox ...”而不是“ the quick brown fix jum ...”。是否有一种简单的方法可以实现此目的?我尝试将lineBreakMode设置为UILineBreakModeTailTruncation,UILineBreakModeWordWrap和UILineBreakModeTailTruncation | UILineBreakModeWordWrap,但这些都没有达到预期的效果;我也尝试了其NSLineBreak ...同行。

I got this working by customizing this example .

My code uses -[NSString enumerateSubstringsInRange:options:usingBlock:] to find words, and then for each word found I check -[NSString boundingRectWithSize:options:attributes:context:] to find if the string still fits my width limit. If the string up to latest found word goes over my limit, I'd use the last word found as the final word of my truncated string.

Just append ellipsis at the end of string and profit!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM