gpt4 book ai didi

cocoa - 为什么 -[NSTextStorage ReplaceCharactersInRange : withAttributedString:] sometimes, 有时不支持其他字符集中的字体?

转载 作者:行者123 更新时间:2023-12-03 16:11:09 24 4
gpt4 key购买 nike

我正在尝试诊断 UKSyntaxColoredTextDocument 0.4 http://www.zathras.de/angelweb/blog-uksctd-oh-four.htm 中的问题其中实际使用的字体与您指定的字体不同的文本会在您键入时消失。 (您可以下载并尝试这个很酷的实用程序,亲自看看这个问题......)

这是背景:这是一些语法着色代码,会在您键入时重新着色。它效果很好,但如果您输入的某些字符不属于该 TextView 的字体集(例如 Monaco、Helvetica)...例如,符号字符或日语中的某些字符,实际上使用 ZapfDingbatsITC 或HiraKakuProN-W3 显示它,那么这些字符在您键入时不会显示。

假设您有一些如下文本:fdsafd[☀]sfds‡[☀☀☀][日本语]...

如果将其粘贴到文本字段中,并从弹出窗口中切换语法着色,则会调用 oldRecolorRange:,其中包含以下行:

[[textView textStorage] replaceCharactersInRange: range withAttributedString: vString];

在这里,事情的表现正如我所期望的那样。 ASCII 文本、符号和日语文本均可见。 [textView textStorage] 的值从开始到结束都是这样的:(这是 gdb 的输出;它不显示 unicode 字符,不用担心。)

df{    NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj=0x001a4970, spc=6.33";}?{    NSFont = "ZapfDingbatsITC 20.00 pt. P [] (0x001ae720) fobj=0x001bb370, spc=5.56";}fdsafd[{    NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj=0x001a4970, spc=6.33";}?{    NSFont = "HiraKakuProN-W3 20.00 pt. P [] (0x001b59e0) fobj=0x001bb600, spc=6.66";}]sfds[{...

...即使将新值设置为

dffdsafd[?]sfds[???][???] Nihddfdfffdfdd{    NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj=0x001a4970, spc=6.33";}

换句话说,显示该字符串所需的“外来”字体会以某种方式自动保留,即使替换字符串中未指定字体。

但是,当您一次输入一个字符时,在方法 recolorRange: 中对 replaceCharactersInRange:withAttributedString: 的不同调用会产生一个属性字符串,该字符串为 < em>仅在基本字体中——没有为我们添加外文字体,所以超出主字体范围的字符根本不可见!

dffdsafd[?]sfds[???][???] Nihddfdfffdfddx{    NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj=0x001a4970, spc=6.33";}

知道为什么这种方法在一种情况下以一种方式工作,而在另一种情况下却不起作用吗?是否可以通过某种开关来向 NSTextStorage/NSAttributedString 提示我们希望文本显示外来字符?

最佳答案

尝试 [[textView textStorage] fixFontAttributeInRange:range];在 recolorRange 中的 [[textView textStorage]replaceCharactersInRange: range withAttributedString: vString] 之后:

我认为问题是为了响应 processEditing 再次编辑文本存储,这已经是编辑的尾部了。 -[NSTextStorage processEditing] 使用 fixFontAttributeInRange:,但由于您尝试再次编辑,所以出现了问题并且修复行为被删除。

当您对整个文档执行此操作时,会有一个正常的 beginEditing/endEditing/processEditing 序列,这会导致 fixFontAttributeInRange: 被愉快地调用。

关于cocoa - 为什么 -[NSTextStorage ReplaceCharactersInRange : withAttributedString:] sometimes, 有时不支持其他字符集中的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/678620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com