首页 > 解决方案 > UITextView在更改字体后呈现奇怪的字符

问题描述

所以我有一个UITextView/NSTextView通过NSMutableAttributedString. 我注意到,当文本字体间距的一个小节发生变化时,一些奇怪的字符会在字体发生变化的地方呈现,请参见此 gif:

奇怪的字符

至少我NSMutableAttributedString用以下块更新了一部分

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

在奇怪的字符之前:

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\"";
}

当奇怪的字符正在渲染时:

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

有没有遇到过类似的奇怪工件和问题?

标签: iosswiftuikit

解决方案


使用时,NSTextStorage如果在对范围进行样式设置后不使用标记范围,NSTextStroage.edited则最终可能会出现一些这种伪影。


推荐阅读