2

問題があり、UIButton のタイトルにテキストを割り当てる必要があります。ボタンの改行モードを NSLineBreakByCharWrapping に設定して、文字列が各行の末尾の文字だけで区切られるようにします。ただし、単語の連続性を示すために、行末にハイフンを挿入する必要があります。これが私が試したことです-

    // Initialize the button

    titleButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleButton.titleLabel.backgroundColor = [UIColor yellowColor];
    [titleButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];

    // Make the string here
    NSMutableString *titleString = [[NSMutableString alloc] initWithString:@"abcdefghijklmnopqrs tuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"];
        // Insert hyphens 
        int titleLength = [titleString length];
        int hyphenIndex = 19;
        while (hyphenIndex<titleLength) { 
            UniChar charatIndex = [titleString characterAtIndex:hyphenIndex];
            if ((charatIndex - ' ') != 0) { // Check if its not a break bw two words
                [titleString insertString:@"-" atIndex:hyphenIndex]; // else insert an hyphen to  indicate word continuity
            }
            hyphenIndex += 19; //Since exactly 20 char are shown in single line of the button's label. 
        }
        //Set the hyphenated title for the button
        [titleButton setTitle:titleString forState:UIControlStateNormal];
        [titleString release];

これは私が得ることができる最も近いものです。

ここに画像の説明を入力

どんな助けでも大歓迎です。

4

4 に答える 4

0

NSParagraphStyle と NSAttributedString を使用してハイフンで壊れる UILabel を取得する方法については、こちらの回答を参照してください: https://stackoverflow.com/a/19414663/196358

于 2013-10-16T21:58:40.093 に答える
0

ハイフンを貼り付けることはあまり良い考えではありません。必要に応じてハイフンを追加するよりも、ボタンの幅に合うサイズのパーツで文字列を分割する必要があります

[String sizeWithFont:font constrainedToSize:Size lineBreakMode:LineBreakMode]

主なアイデアは、最初の文字列の一部を取得し (単語が壊れている場合はハイフンを追加)、その幅がボタンの幅に収まるかどうかを確認することです。幅が小さい場合は、より大きな部分を試してください。そうでない場合は、さらに部分を処理します

于 2013-01-09T10:57:51.007 に答える
0

2番目の単語の長さは、行の長さ(ボタンの幅)よりも長いため、これが発生します。

于 2013-01-09T10:45:15.453 に答える