xcode iOS - 如何快速使用`NSMutableString`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26554643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - How to use `NSMutableString` in swift
提问by Steve Gailey
I have seen this Objective-C code but I'm struggling to do the same in swift:
我已经看过这个 Objective-C 代码,但我正在努力快速地做同样的事情:
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;
I'm trying to change the fonts in a NSMutableAttributedString
by iterating over each of the attributes. I'm more than happy to hear that there is a better way but if anyone can help me translate the above I'd be more than greatful.
我试图NSMutableAttributedString
通过迭代每个属性来更改 a 中的字体。我很高兴听到有更好的方法,但如果有人能帮我翻译上述内容,我会非常感激。
回答by Aaron Brager
Here's a basic implementation. It seemed pretty straightforward to me, and you didn't provide your attempt, so I'm not sure if you have something similar and there's a problem with it, or if you're just new to Swift.
这是一个基本的实现。对我来说这似乎很简单,而且你没有提供你的尝试,所以我不确定你是否有类似的东西并且它有问题,或者你是否只是 Swift 的新手。
One difference is that this implementation uses optional casting (as?
), which I did to demonstrate the concept. In practice, this doesn't need to be optional since NSFontAttributeName
is guaranteed to provide a UIFont
.
一个区别是这个实现使用了可选的强制转换 ( as?
),我这样做是为了演示这个概念。在实践中,这不需要是可选的,因为NSFontAttributeName
它保证提供一个UIFont
.
var res : NSMutableAttributedString = NSMutableAttributedString(string: "test");
res.beginEditing()
var found = false
res.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, res.length), options: NSAttributedStringEnumerationOptions(0)) { (value, range, stop) -> Void in
if let oldFont = value as? UIFont {
let newFont = oldFont.fontWithSize(oldFont.pointSize * 2)
res.removeAttribute(NSFontAttributeName, range: range)
res.addAttribute(NSFontAttributeName, value: newFont, range: range)
found = true
}
}
if found == false {
}
res.endEditing()
回答by ZAZ
Hope it helps!
希望能帮助到你!
var res : NSMutableAttributedString = self.richTextEditor.attributedText!
res.beginEditing()
var found : bool = false;
res.enumerateAttribute(NSFontAttributeName,inRange:NSMakeRange(0, res.length),options:0, usingBlock(value:AnyObject!, range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) -> Void in {
if (value) {
let oldFont = value as UIFont;
let newFont = oldFont.fontWithSize(oldFont.pointSize * 2)
res.removeAttribute(NSFontAttributeName , range:range)
res.addAttribute(NSFontAttributeName value:newFont range:range)
found = true
}
})
if !found {
// No font was found - do something else?
}
res.endEditing()
self.richTextEditor.attributedText = res;