简体   繁体   English

NSMutableAttributedString知道在哪个文本上更改颜色

[英]NSMutableAttributedString to know where color is changed on which text

I am doing tagging feature like facebook and I can tag user already. 我正在做类似Facebook的标记功能,我已经可以标记用户了。 I can show like this. 我可以这样显示。 It is done by this code. 通过此代码完成。

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textView.text];

for (NSString *word in self.tagNameCollection) {
    [string addColor:[UIColor redColor] substring:word];
    [string addBackgroundColor:[Helpers getFromRGB:135 green:206 blue:250] substring:word];
}

So, I have NSMutableAttributedString. 因此,我有NSMutableAttributedString。 Can I know where I have changed colour, font from my NSMutableAttributedString? 我可以从NSMutableAttributedString知道在哪里更改了颜色,字体吗? May I know how to do? 我可以知道怎么做吗?

在此处输入图片说明

You can use enumerateAttributesInRange:options:usingBlock: to get what are the attributes of your NSAttributedString . 您可以使用enumerateAttributesInRange:options:usingBlock:来获取NSAttributedString的属性是NSAttributedString

Example: 例:

[attributedString enumerateAttributesInRange:NSMakeRange(0, [attributedString length])
                                     options:0
                                  usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
if ([attributes objectForKey:NSForegroundColorAttributeName])
    NSLog(@"Found ForeGround Color: %@ in range %@", [attributes objectForKey:NSForegroundColorAttributeName], NSStringFromRange(range));
if ([attributes objectForKey:NSFontAttributeName])
    NSLog(@"Found Font: %@ in range %@", [attributes objectForKey:NSFontAttributeName], NSStringFromRange(range));
if ([attributes objectForKey:NSBackgroundColorAttributeName])
    NSLog(@"Found Background Color: %@ in range %@", [attributes objectForKey:NSBackgroundColorAttributeName], NSStringFromRange(range));

}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM