2

属性テキストをラベルに設定しようとしています。属性は、色だけでなくフォントも機能しているようです。

私が直面している唯一の問題は、行の折り返しです。UILabel のサイズは (200,300) で、numberofLines=0 です。したがって、これで行を折り返す必要がありますが、そうではありません。

   NSMutableString *title=[[NSMutableString alloc] init];
    NSRange range1;
    NSRange range2;
    NSRange range3;



        NSString *str1=@"ABCD EFGHI klm";
        [title appendString:str1];
        range1=NSMakeRange(0, str1.length);


        NSString *str2=@"PQRSSSS ";
        [title appendString:str2];
        range2=NSMakeRange(range1.length, str2.length);


        NSString *str3=@"1235 2347 989034 023490234 90";
        [title appendString:str3];
        range3=NSMakeRange(range2.location+range2.length, str3.length);


    NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];

    self.myTextLabel.attributedText=attributeText;

高さが300なのにUILabelはこのように表示されます。

ABCD EFGHI klm PQRSSSS 1235 234 ...

4

5 に答える 5

5

必要なのは NSParagraphStyle 属性です:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 1;

//Now add this to your attributes dictionary for the key NSParagraphStyleAttributeName eg, 

@{NSParagraphStyleAttributeName:paragraphStyle,...}

関係のない話ですが、最新の Objective-c 形式で辞書を作成する方がよいことがわかっています。そうしないとメンターに怒られます。それは次のようになります。

[attributeText setAttributes:@{NSForegroundColorAttributeName:color1, NSFontAttributeName:[self getStlylishItalicFont:13.0], NSParagraphStyleAttributeName:paragraphStyle, }];
//The trailing comma in the dictionary definition is not at typo it is important.
于 2013-07-17T06:42:48.587 に答える
2

UILabel の改行モード属性を、次のように希望するものに設定してください。

UILabel.lineBreakMode = NSLineBreakByWordWrapping;

または、Interface Builder を使用している場合は、そこで行うことができます。

于 2013-07-17T05:54:22.677 に答える
0

Why can't you set the numberoflines to 1.Because wrapping makes sense but number of lines 0 doesnt make sense.. and also you have to set proper frame for label.

于 2013-07-17T04:39:30.613 に答える