【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