I'm having a trouble with emoji in a custom NSTextStorage subclass. The class does not store any attributes passed to it. Instead, it generates its own:
override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
if range != nil {
range.memory = NSMakeRange(0, self.string.length)
}
let attributes = [
NSFontAttributeName: NSFont.systemFontOfSize(14)
]
return attributes
}
override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
// does nothing
}
This mostly works fine. However, if there are any emoji in the string, they simply don't show up. In examining the calls that the NSTextView makes to the text storage, it appears that the text view tries to set the font attribute of any emoji ranges to the AppleColorEmoji font whenever they appear. That's fine if you're relying on the text view as the source of "attribute truth", but I don't want my program to work like that. The text storage, in my case, needs to be the sole vendor of any attributes. It can't listen to anything that the text view sends it, attribute-wise.
Am I going to have to manually detect any emoji in my string and set the AppleColorEmoji font manually? Or is there a better way? I've already tried using fallback fonts and automatically searching for fonts that contain missing characters, but emoji don't seem to be covered using those methods.