Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSLineBreakByWordWrapping在哪些字符上中断?_Ios_Objective C_Uilabel - Fatal编程技术网

Ios NSLineBreakByWordWrapping在哪些字符上中断?

Ios NSLineBreakByWordWrapping在哪些字符上中断?,ios,objective-c,uilabel,Ios,Objective C,Uilabel,短版 从文档中: NSLineBreakByWordWrapping 换行发生在单词边界处,除非单词本身不能放在一行上 所有单词边界字符的集合是什么 较长版本 我有一组UILabel,其中包含文本,有时包括URL。我需要知道URL的确切位置(frame,而不是range),这样我才能使它们可点击。我的数学基本上是有效的,但我必须针对某些角色进行测试: // This code only reached if the URL is longer than the available wid

短版

从文档中:

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]

这是说,如果你做了很多这件事,你可能想考虑使用<代码> CTFRAMESETER < /代码>来完成你的布局,而不是<代码> uILabel。然后您可以使用

CTRunGetImageBounds
计算实际的矩形。您不必计算分词,因为
CTFrame
已经为您完成了这项工作(并且保证是相同的算法)。

我认为这将包括许多不被视为“单词边界”的字符,如%和=。UILabel和NSString的
drawInRect:withFont:lineBreakMode:
消息中断都不在其上。我正在使用
drawInRect
在屏幕上绘制文本,然后在选中表格单元格时将tappable
UILabel
s放在URL文本上。(使用绘制的文本时,tableview的滚动速度比使用数以百万计的
UILabel
s时快得多。)