iOS - How to use `NSMutableString` in swift

旧街凉风 提交于 2019-12-05 01:46:26

问题


I have seen this Objective-C code but I'm struggling to do the same in 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;

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.


回答1:


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.

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.

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()



回答2:


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;


来源:https://stackoverflow.com/questions/26554643/ios-how-to-use-nsmutablestring-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!