2013-02-27 55 views
2

短版NSLineBreakByWordWrapping打破了哪些字符?

從文檔:

NSLineBreakByWordWrapping

包裝發生在單詞邊界,除非這個詞本身不適合在一行。

什麼是所有字邊界字符的集合?

加長版

我有一組UILabels,其中包含文本,有時還包括網址。我需要知道網址的確切位置(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中看到的其他字符。是否有完整的角色列表我應該打破?

回答

0

我建議使用「非字母數字」作爲測試。

[[NSCharacterSet alphanumerCharacterSet] invertedSet] 

這就是說,如果你做了很多這方面,你可能要考慮使用CTFramesetter做你的佈局,而不是UILabel。然後,您可以使用CTRunGetImageBounds來計算實際折彎。你不必計算你的分詞,因爲CTFrame已經爲你做了這個(並且它將被保證是相同的算法)。

+1

我認爲這將包括許多不被視爲「字邊界」的字符,如%和=。那些UILabel和NSString的'drawInRect:withFont:lineBreakMode:'消息都沒有中斷。 – 2013-02-27 23:41:23

+0

我使用'drawInRect'在屏幕上繪製文本,然後在選中表格單元格時在URL文本上放置可定位的'UILabel'。 (tableview使用繪製的文本更快地滾動,而不是數百萬個「UILabel」。 – 2013-02-27 23:42:56