繁体   English   中英

iOS - 将所有NSMutableAttributedString属性复制到另一个NSMutableAttributedString

[英]iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString

我想将一个NSMutableAttributedString所有属性复制到一个新属性中。 我试过的代码是这样的:

[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;
        }
    }];

代码显然是不完整的,看起来它试图只为字体做。 我想遍历每个属性并将其添加到新的NSMutableAttributedString变量中。 更新:我的问题是如何将一个NSMutableAttributedString所有属性应用于另一个NSMutableAttributedString 我们可以somehow:attribute:atIndex:effectiveRange使用此方法somehow:attribute:atIndex:effectiveRange

NSMutableAttributedString (和NSAttributedString )符合NSCopying 所以你应该能够做到这一点:

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

暂无
暂无

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

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