2

短縮版

ドキュメントから:

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 で見られる他の文字では壊れません。ブレークすべきキャラクターの完全なリストはありますか?

4

1 に答える 1

0

テストとして「英数字ではない」を使用することをお勧めします。

[[NSCharacterSet alphanumerCharacterSet] invertedSet]

CTFramesetterとはいえ、これを頻繁に行う場合は、 の代わりに を使用してレイアウトを行うことを検討することをお勧めしますUILabelCTRunGetImageBounds次に、実際の四角形を計算するために使用できます。単語区切りを計算する必要はありません。CTFrameこれは既に行われているためです (同じアルゴリズムであることが保証されます)。

于 2013-02-27T17:53:46.740 に答える