13

NSMutableAttributedStringを使用して複数行のUILabelを作成しようとしています。これは、次のように完全な文字列に属性を割り当てると正常に機能します。

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];

contentLabel.attributedText = string;

しかし、私が必要としているのは、NSAttributedStringのさまざまなサブ範囲にいくつかの属性を適用することです(特定の単語を太字にするため)。

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)];

contentLabel.attributedText = string;

私が見つけたのは、これを行うと、テキストがラベルの複数の行にレンダリングされなくなることです。これは、ラベルのフレームの垂直方向の中央に配置された1本の線としてレンダリングされます。

ここに欠けているものはありますか?

4

2 に答える 2

4

質問のコメントで説明されているように、質問で提示されたコードは実際には正しく機能し、意図したとおりに属性付き文字列をレンダリングします。(問題は私のコードの他の場所にありました)

于 2014-05-07T21:08:29.037 に答える
1

私はUITextViewを頻繁に使用し、同様の方法で属性付けられたテキストを割り当てます。

        // creiamo textView descrizione
    UITextView *description = [[UITextView alloc] init];
    description.frame = CGRectMake(20, 453, 500, 190);

    NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                     NSBackgroundColorAttributeName: [UIColor clearColor],
                                     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:22.0],
                                     };

    NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                        NSBackgroundColorAttributeName: [UIColor clearColor],
                                        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10],
                                        };

    NSDictionary *attribute3 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                           NSBackgroundColorAttributeName: [UIColor clearColor],
                                           NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]
                                           };

    NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@ \n\n%@ \n     ", string1, string2 , string3, string4]];

    [info setAttributes:attribute1 range:NSMakeRange(0, ([string1 length]))];
    [info setAttributes:attribute2 range:NSMakeRange(([string1 length]),([string2 length]+[string3 length]))];
    [info setAttributes:attribute3 range:NSMakeRange(([string1 length] [string2 length]+[string3 length]), [string4 length])];

    description.attributedText = info;
    description.backgroundColor = [UIColor clearColor];
    description.editable = NO;
    description.textColor = [UIColor blackColor];
    [viewInfo addSubview:description];
    [description sizeToFit];
于 2013-08-14T07:23:53.750 に答える