Trim first character in NSMutableAttributedString

青春壹個敷衍的年華 提交于 2019-12-24 02:55:15

问题


I am using NSMutableAttributedString to show attributed string in label. Is there way to trim first character of NSMutableAttributedString without change in attributes.


回答1:


No because one of the attributes of the attributes is the range of the string they effect, and those will become invalid if the string length changes.

The best approach would be to reconstruct the attributed string from scratch, which might be simple or difficult, depending on whether you know the attributes to add.




回答2:


NSMutableAttributedString supports the deleteCharacters(in:NSRange) method:

@IBOutlet weak var topLabel: NSTextField!
@IBOutlet weak var bottomLabel: NSTextField!
...
    let textAttributes : [String : Any] = [
        NSForegroundColorAttributeName : NSColor.blue,
        NSFontAttributeName : NSFont(name: "Menlo", size: 12.0)!
    ]
    let text = NSMutableAttributedString(string: "ABCDEF",
                                         attributes: textAttributes)
    topLabel.attributedStringValue = text
    text.deleteCharacters(in: NSMakeRange(0,1))
    bottomLabel.attributedStringValue = text
...


来源:https://stackoverflow.com/questions/26383009/trim-first-character-in-nsmutableattributedstring

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