我需要的只是循环遍历所有属性NSAttributedString
并增加字体大小。到目前为止,我已成功循环并操作属性,但无法保存回NSAttributedString
。我注释掉的行对我不起作用。怎么救回来?
NSAttributedString *attrString = self.richTextEditor.attributedText;
[attrString enumerateAttributesInRange: NSMakeRange(0, attrString.string.length)
options:NSAttributedStringEnumerationReverse usingBlock:
^(NSDictionary *attributes, NSRange range, BOOL *stop) {
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
UIFont *font = [mutableAttributes objectForKey:NSFontAttributeName];
UIFont *newFont = [UIFont fontWithName:font.fontName size:font.pointSize*2];
[mutableAttributes setObject:newFont forKey:NSFontAttributeName];
//Error: [self.richTextEditor.attributedText setAttributes:mutableAttributes range:range];
//no interfacce for setAttributes:range:
}];
像这样的东西应该有效:
NSMutableAttributedString *res = [self.richTextEditor.attributedText mutableCopy];
[res beginEditing];
__block BOOL found = NO;
[res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];
[res removeAttribute:NSFontAttributeName range:range];
[res addAttribute:NSFontAttributeName value:newFont range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[res endEditing];
self.richTextEditor.attributedText = res;
在此刻res
有一个新的属性字符串,所有字体都是原始大小的两倍。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)