短版
从文档:NSLineBreakByWordWrapping
包装会在单词边界处发生,除非单词本身不在一行上。
所有单词边界字符的集合是什么?
较长版本
我有一组UILabel,其中包含文本,有时还包括URL。我需要知道URL的确切位置(frame
,而不是range
),以便使它们可被点击。我的数学大部分都可以用,但是我必须对某些字符进行测试:
// This code only reached if the URL is longer than the available width
NSString *theText = // A string containing an HTTP/HTTPS URL
NSCharacterSet *breakChars = [NSCharacterSet characterSetWithCharactersInString:@"?-"];
NSString *charsInRemaininsSpace = // NSString with remaining text on this line
NSUInteger breakIndex = NSNotFound;
if (charsInRemaininsSpace)
breakIndex = [charsInRemaininsSpace rangeOfCharacterFromSet:breakChars
options:NSBackwardsSearch].location;
if (breakIndex != NSNotFound && breakIndex != theText.length-1) {
// There is a breakable char in the middle, so draw a URL through that, then break
// ...
} else {
// There is no breakable char (or it's at the end), so start this word on a new line
// ...
}
我的NSCharacterSet中的字符是?和-我发现NSLineBreakByWordWrapping中断了。它不会破坏我在%和=之类的URL中看到的其他一些字符。是否有我应该打断的完整字符列表?
最佳答案
我建议使用“非字母数字”作为测试。
[[NSCharacterSet alphanumerCharacterSet] invertedSet]
就是说,如果您要执行很多操作,则可能要考虑使用
CTFramesetter
而不是UILabel
进行布局。然后,您可以使用CTRunGetImageBounds
来计算实际的rect。您无需计算单词中断,因为CTFrame
已经为您完成了这一工作(并且将保证是相同的算法)。
关于ios - NSLineBreakByWordWrapping中断哪些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118623/