gpt4 book ai didi

ios - 在 NSAttributedString 上有多个具有不同属性的范围

转载 作者:行者123 更新时间:2023-11-28 21:50:16 24 4
gpt4 key购买 nike

我有一个多行 UILabel,我想增加它的行高,但我也希望它的一部分是不同的颜色,只有行高可以正常工作。但是,一旦我尝试更改某个范围内的颜色,它就会恢复到库存外观,也没有线条..

有人给小费吗?这是在内容 setter 中完成的。


- (void)setContent:(NSString *)content {
_content = content;

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.content];
NSMutableAttributedString *mutableAttrString = [attributedString mutableCopy];

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 5.0f];

NSDictionary *attributes = @{
NSFontAttributeName: [UIFont fontWithName:@"BentonSans-Regular" size:16.0],
NSParagraphStyleAttributeName: paragraphStyle
};
NSDictionary *colorAttributes = @{
NSForegroundColorAttributeName: [UIColor redColor]
};
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
[mutableAttrString addAttributes:colorAttributes range:NSMakeRange(4, 8)];


[self.label setAttributedText: mutableAttrString];
}

谢谢!

最佳答案

NSRangeFromString 函数需要一个类似@"{3,10}" 的字符串。换句话说,它需要一个包含两个数字的字符串,这两个数字指定范围的起始位置和长度。我怀疑 content 字符串不是那样的字符串。

所以这一行

[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];

应该是

[mutableAttrString addAttributes:attributes range:NSMakeRange(0,mutableAttrString.length)];

关于ios - 在 NSAttributedString 上有多个具有不同属性的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599174/

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