gpt4 book ai didi

ios - NSAttributedString 包装问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:13 24 4
gpt4 key购买 nike

我正在尝试将属性文本设置为标签。这些属性似乎与字体和颜色有关。

我面临的唯一问题是换行。 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;

UILabel 是这样显示的,即使高度是 300。

ABCD EFGHI klm PQRSSSS 1235 234 ...

最佳答案

你需要的是 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.

关于ios - NSAttributedString 包装问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com