简体   繁体   中英

iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString

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

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

NSMutableAttributedString *mutableCopy = attrStr.mutableCopy;
NSAttributedString *immutableCopy = attrStr.copy;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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