2013-08-13 4 views
5

NSAttributedStringを使用して、以下のように三角形内にテキストを作成しようとしました。なぜNSLineBreakByWordWrappingは単語を分割するのですか?

http://postimg.org/image/rp9fukgaj/

Iは、定義された形状内のテキストをフィットするための "NSLineBreakByWordWrapping" 方法を使用しました。しかし、テキストの最初の単語分割と私はそれのための解決策を見つけることができませんでした。私はテキストを分割するのではなく、次の行にジャンプしたい。どうやってやるの?

私は以下のコードを使用しました。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.view.backgroundColor=[UIColor blackColor]; 
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"HelveticaNeue-Regular", 15.0f, NULL);  
    NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new]; 
    paragraph.headIndent = 10; 
    paragraph.firstLineHeadIndent = 10; 
    paragraph.tailIndent = -10; 
    paragraph.lineBreakMode = NSLineBreakByWordWrapping; 
    paragraph.alignment = NSTextAlignmentCenter; 
    paragraph.paragraphSpacing = 15;  
    NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)fontRef, (NSString *)kCTFontAttributeName, (id)[[UIColor whiteColor] CGColor], (NSString *)(kCTForegroundColorAttributeName),paragraph,NSParagraphStyleAttributeName, nil];  
    CFRelease(fontRef); 
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Initially, now I create the paragraph style and apply it to the first character. Note how the margins are dictated: the tailIndent is negative, to bring the right margin leftward, and the firstLineHeadIndent must be set separately, as the headIndent does not automatically apply to the first line." attributes:attrDictionary]; 
    [(CustomView *)[self view] setAttString:attString]; 
} 
+0

最初の単語が収まりませんので、すべてを下に移動するために段落区切りで始めてください。 Text Kitを使用して描画し、レイアウトマネージャをオーバーライドして、これが起こらないようにすることもできます(またはハイフネーションにすることもできます)。 – matt

答えて

0

単語が与えられた行に収まらないだろう、明らか場合、NSLineBreakModeためdocumentationをチェックアウト、ワードが収まるように分割されます。テキストが静的な場合は、改行文字を挿入して三角形の最初の部分をスキップするだけです。さもなければ、あなたはこの限界を克服する方法を理解するために賢明でなければならないでしょう。

+0

改行文字を挿入することでこの問題を克服しました。簡単な最初の単語長アルゴリズムで、私はこの問題を管理しました。私はそれが最良の解決策ではないことを知っていますが、私の場合にはうまくいきました私の記事に感謝してくれてありがとう。 – serdarsenol

関連する問題