Trim the first character in NSMutableAttributedString

I am using NSMutableAttributedString to show an attribute string in a label. Is there a way to trim the first character of NSMutableAttributedString without changing the attributes.

+2
source share
2 answers

No, because one of the attributes of the attributes is the range of the string in which they operate, and those become invalid if the length of the string changes.

A better approach would be to restore the attribute string from scratch, which can be simple or complex, depending on whether you know the attributes you are adding.

+3
source

NSMutableAttributedStringsupports the method deleteCharacters(in:NSRange):

@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
...
0
source

Source: https://habr.com/ru/post/1688787/


All Articles