【发布时间】:2014-12-20 16:31:27
【问题描述】:
我已经看过这段 Objective-C 代码,但我很难在 swift 中做同样的事情:
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;
我正在尝试通过迭代每个属性来更改NSMutableAttributedString 中的字体。我很高兴听到有更好的方法,但如果有人能帮我翻译以上内容,我会非常感激。
【问题讨论】:
-
你能展示你到目前为止的尝试吗?什么不工作?
标签: ios xcode swift font-size nsmutableattributedstring