短縮版
ドキュメントから:
NSLineBreakByWordWrapping
単語自体が 1 行に収まらない場合を除き、折り返しは単語の境界で発生します。
すべての単語境界文字のセットは?
ロングバージョン
テキストを含み、場合によっては URL を含む UILabels のセットがあります。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 で見られる他の文字では壊れません。ブレークすべきキャラクターの完全なリストはありますか?