修剪NSMutableAttributedString中的第一个字符

我正在使用NSMutableAttributedString显示标签中的属性string。 有没有办法修剪NSMutableAttributedString的第一个字符,而不改变属性。

否,因为这些属性的其中一个属性是它们影响的string范围,如果string长度发生变化,这些属性将变为无效。

最好的方法是重build从头开始的属性string,这可能是简单的或困难的,取决于您是否知道要添加的属性。

NSMutableAttributedString支持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 ...