7

I'd like to copy all attributes from one NSMutableAttributedString into a new one. The code i've tried is this:

[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
           // UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [_label.attributedText
            [attrStr removeAttribute:NSFontAttributeName range:range];
            [attrStr addAttribute:NSFontAttributeName value:newFont range:range];
            //found = YES;
        }
    }];

the code is clearly incomplete and it looks like its attempting to do it for fonts only. I'd like to loop through every attribute and add it into a new NSMutableAttributedString variable. Update: my question is how do I apply all attributes of one NSMutableAttributedString to another NSMutableAttributedString ? Can we use this method somehow:attribute:atIndex:effectiveRange

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

-3

NSMutableAttributedString (and NSAttributedString) conforms to NSCopying. So you should just be able to do this:

NSMutableAttributedString *mutableCopy = attrStr.mutableCopy;
NSAttributedString *immutableCopy = attrStr.copy;
Dima
  • 23,484
  • 6
  • 56
  • 83
  • 3
    i'd like to copy the attributes not the entire object. Anyway i can get all the attribures in a dictionary perhaps and then copy them to all to a new attributedString ? – j2emanue May 15 '15 at 12:27