I'm subclassing NSTextStorage
to do some link highlighting and I've read as much as I can on the topic. Everything works fine until I type the emoji character.
My subclass:
private let ims = NSMutableAttributedString()
override var string: String {
return ims.string
}
override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return ims.attributesAtIndex(location, effectiveRange: range)
}
override func replaceCharactersInRange(range: NSRange, withString str: String) {
ims.replaceCharactersInRange(range, withString: str)
self.edited(.EditedCharacters, range: range, changeInLength:(str as NSString).length - range.length)
}
override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
ims.setAttributes(attrs, range: range)
self.edited(.EditedAttributes, range: range, changeInLength: 0)
}
Nothing complicated. Then, when entering the infamous character it switches to Courier New for some random reason:
Now I'm picking on the character, there are others that cause this maddness too. I've queried the font as I type and it goes from System > Apple Emoji > Courier New.
I've also tried setting the font from within processEditing()
which semi solves the problem, It causes an extra space to be added in (not in the simulator though). And I'm hardcoding a value == bad.
Ultimate Question:
What am I doing wrong? I don't see this problem with other people's implementations where I'm certain developers have subclassed NSTextStorage.
Note: I can confirm that in objc.io's demo app the same issue is present.