0

I am doing tagging feature like facebook and I can tag user already. 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. Can I know where I have changed colour, font from my NSMutableAttributedString? May I know how to do?

enter image description here

Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120

1 Answers1

2

You can use enumerateAttributesInRange:options:usingBlock: to get what are the attributes of your 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));

}];
Larme
  • 24,190
  • 6
  • 51
  • 81
  • Thanks. It seem okay. Do I always need to keep NSMutableAttributedString and update when something is changed in my uitextview? Can I get NSMutableAttributedString from my uitextview? If I do like this, property of NSMutableAttributedString is gone. NSMutableAttributedString *muStr = [[NSMutableAttributedString alloc]initWithString:self.inputToolBarViewInDetail.textView.text]; – Khant Thu Linn Mar 02 '15 at 09:21
  • If you want to know the changes, you may have to keep a reference from the previous one, or maybe check the `UITextViewDelegate` method when text is modified. – Larme Mar 02 '15 at 10:59