【macOS】【Swift】【RTF】黑色文字在macOS深色外观下看不清的解决方法

【macOS】【Swift】【RTF】黑色文字在macOS深色外观下看不清的解决方法

遇到这个问题,

Cursor高效率地忙乎了一个小时,给出了很多方案并做了修改,但都没生效。

kimi还给出了:在深色外观下,将黑色文字转成白色;保存时,再反转的方法。

我都感觉:不是我当前App所需要的效果。

==========

最后,找到很简单的方法,一行代码即可。

当macOS的外观设置为深色时,

RTF文件读取后,如果文字也是黑色,则无法看清楚。

简单的解决方法是:

复制代码
/// AppKit封装,SwiftUI中显示NSAttributedString
struct RTFTextView: NSViewRepresentable {
    let attributedString: NSAttributedString
    func makeNSView(context: Context) -> NSTextView {
        let textView = NSTextView()
        textView.isEditable = false
        textView.textStorage?.setAttributedString(attributedString)
        
        //设置RTF文字的背景:透明
        //textView.backgroundColor = NSColor.clear
        
        //设置RTF文字的背景:浅灰色
        textView.backgroundColor = NSColor.lightGray
        
        //设置RTF文字的背景:保持浅色背景(不用macOS的外观,防止黑色外观时,黑色文字看不清)
        //textView.appearance = NSAppearance(named: .aqua)
        
        return textView
    }
    func updateNSView(_ nsView: NSTextView, context: Context) {
        nsView.textStorage?.setAttributedString(attributedString)
    }
}

shense

相关推荐
pe7er3 天前
macOS 应用无法打开(权限问题)解决方案
macos·mac
harmful_sheep6 天前
mac生效的终端查看
macos
iOS门童6 天前
macOS 应用"已损坏"无法打开?一文搞懂 Gatekeeper 与解决方案
macos
NPE~7 天前
[工具分享]Maccy —— 优雅的 macOS 剪贴板历史管理工具
macos·教程·工具·实用工具
差不多程序员7 天前
Mac安装OpenClaw-cn保姆级教程
macos
dzl843947 天前
mac 安装python
开发语言·python·macos
Bruce_Liuxiaowei7 天前
在 macOS 上通过 Docker 本地安装 OpenClaw 完整教程
macos·docker·容器·openclaw
阿捏利7 天前
详解Mach-O(十五)Mach-O __DATA_CONST
macos·ios·c/c++·mach-o
ShikiSuen7 天前
macOS 的 CpLk 中英切换卡顿的元凶在 InputMethodKit 本身
macos
xiayutian_c7 天前
如虎添翼-MacOS
macos