Search code examples
iosswiftuikit

UITextView rendering strange characters after changing font


So I've got a UITextView/NSTextView that dynamically updates it's styling via an NSMutableAttributedString. I'm noticing that when a subsection of the text's font's spacing changes some strange characters render where the font was changed, see this gif:

weird characters

Minimally I'm updating a section of the NSMutableAttributedString with the following block

attr.setAttributes([
    .foregroundColor : textColor,
    .font : UIFont.monospacedSystemFont(ofSize: fontSize, weight: .thin),
], range: range)

Before the weird characters:

Hello
{
    NSColor = "Catalog color: System labelColor";
    NSFont = "\".AppleSystemUIFont 13.00 pt. P [] (0x7fed6ef77420) fobj=0x7fed6ef77420, spc=3.58\"";
}'''
nice stuff
'''{
    NSColor = "Catalog color: System labelColor";
    NSFont = "\".AppleSystemUIFontMonospaced-Regular 13.00 pt. P [] (0x7fed6ef7aa50) fobj=0x7fed6ef7aa50, spc=8.04\"";
}
Hello{
    NSColor = "Catalog color: System labelColor";
    NSFont = "\".AppleSystemUIFont 13.00 pt. P [] (0x7fed6ef77420) fobj=0x7fed6ef77420, spc=3.58\"";
}

When the weird characters are rendering:

Hello
'''
nice stuff
''
Hello{
    NSColor = "Catalog color: System labelColor";
    NSFont = "\".AppleSystemUIFont 13.00 pt. P [] (0x7fed6ef77420) fobj=0x7fed6ef77420, spc=3.58\"";
}

Has any experienced weird artifacts and issues like this?


Solution

  • When using NSTextStorage if you don't mark the range with NSTextStroage.edited after styling the ranges you can end up with some of this artifacting.