Ios UITextView在更改字体后呈现奇怪字符

Ios UITextView在更改字体后呈现奇怪字符,ios,swift,uikit,Ios,Swift,Uikit,因此,我有一个UITextView/NSTextView,它通过nsmutableAttributeString动态更新它的样式。我注意到,当文本字体间距的一部分发生变化时,字体发生变化的地方会出现一些奇怪的字符,请参见以下gif: 至少我要用下面的块更新nsmutableAttributeString的一部分 attr.setAttributes([ .foregroundColor : textColor, .font : UIFont.monospacedSystemFo

因此,我有一个
UITextView/NSTextView
,它通过
nsmutableAttributeString
动态更新它的样式。我注意到,当文本字体间距的一部分发生变化时,字体发生变化的地方会出现一些奇怪的字符,请参见以下gif:

至少我要用下面的块更新
nsmutableAttributeString
的一部分

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

有没有遇到过像这样的奇怪的工件和问题?

在使用
NSTextStorage
时,如果您没有使用
nstextstroge标记范围。在设置范围样式后,编辑后的
可以得到一些工件。

您有完整的可复制样本吗?我想知道这是否不是编码的问题,UTF8(默认字符串计数)与UTF16长度(NSRange、nsAttributed String length、NSString length)之间的关系。事实上,我解决了我的困境。谢谢你的建议!