在 NSAttributedString 上有多个具有不同属性的范围
Have multiple ranges with different attributes on NSAttributedString
我有一个多行 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)];
在您的 viewdidLoad 方法中将字符串分配给 self.content :
self.content = @"pass your text ";
// 删除不需要的方法的第一行
- (void)setContent:(NSString *)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];
}
我有一个多行 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)];
在您的 viewdidLoad 方法中将字符串分配给 self.content :
self.content = @"pass your text ";
// 删除不需要的方法的第一行
- (void)setContent:(NSString *)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];
}