当前位置: 首页 > ds >正文

【RichTextEditor】 【分析2】RichTextEditor设置文字内容背景色

【RichTextEditor】 【分析2】RichTextEditor设置文字内容背景色

都说AI Coder的Cursor很牛,也付费用了,
但这个背景色,搞了一天也没改过来。
最后,让它分析该控件的层次结构及文本内容显示的位置。

然后,搞定!
该自定义控件,原来文本内容设置的是按系统的外观模式色系来处理的。
结果,我在读取RTF文档后,由于文字一般都是黑色,
而我的macOS系统又是设置为深色,
结果可想而知:黑色文字,在黑色背景,基本上无法看。
 

我在我开发的工具软件AIBookReader中,
希望用户根据自己的喜好来设置文本内容的背景色,
所以需要提供这个修改功能。

// MARK: - Editor View
private struct EditorView: View {@Binding var document: DemoDocumentlet context: RichTextContext@Binding var zoomScale: CGFloat@Binding var editorViewHeight: CGFloatlet backgroundColor: Colorprivate let logger = Logger(subsystem: "com.demo", category: "Editor")var body: some View {GeometryReader { editorGeometry inScrollView {RichTextEditor(text: $document.text,context: context) {textView in// 设置 NSTextView 的背景色为当前选中的背景色// 在这里设置文字内容的背景色 -2025-5-24-modify-by-goodmaoif let nsTextView = textView as? NSTextView {//nsTextView.backgroundColor = NSColor(backgroundColor)nsTextView.backgroundColor = NSColor.cyan}}.frame(minHeight: editorGeometry.size.height).scaleEffect(zoomScale, anchor: .topLeading).frame(width: editorGeometry.size.width, height: editorGeometry.size.height, alignment: .topLeading).border(Color.red, width: 2).onChange(of: zoomScale) { oldValue, newValue inlet scaledWidth = editorGeometry.size.width * newValuelet scaledHeight = editorGeometry.size.height * newValuelogger.info("缩放区域尺寸变化 - 宽度: \(scaledWidth), 高度: \(scaledHeight), 缩放比例: \(newValue)")}}.frame(maxWidth: .infinity, maxHeight: .infinity).onChange(of: editorGeometry.size) { oldValue, newValue ineditorViewHeight = newValue.heightlogger.info("=== 【红色区域】:编辑区域和缩放控件-VStack 中的 RichTextEditor 高度: \(newValue.height)")}}.frame(maxWidth: .infinity, maxHeight: .infinity)}
}

http://www.xdnf.cn/news/8662.html

相关文章:

  • 第八章:数据库查询优化
  • 上升沿计数 stm32 中断
  • 用service 和 SCAN实现sqlplus/jdbc连接Oracle 11g RAC时负载均衡
  • 在Mac中使用pyenv管理Python版本:从安装到虚拟环境的全流程指南
  • 物联网网关保障沼气发电站安全运行的关键技术解析
  • 文章记单词 | 第111篇(六级)
  • 江科大ADC模数转换hal库实现
  • C++构造函数和析构函数
  • 静态库的使用方法
  • BaseDao指南
  • 生成模型——变分自动编码器(Variational Autoencoders, VAEs)
  • 项目管理进阶:111页 详解华为业务变革框架及战略级项目管理【附全文阅读】
  • LaTeX学习路线
  • 63. 不同路径 II
  • 2.2.1 05年T1复习
  • 1.2 TypeScript 与 JavaScript 的区别
  • Java:堆排序
  • Git教程
  • 龙虎榜——20250523
  • 地形生成原理与实现
  • 【Java】Java元注解
  • 【操作系统】-4.1.8文件共享
  • Unitree 5. GO1 3D打印配件
  • 高通usecase理解
  • 【三维重建】【3DGS系列】【深度学习】3DGS的理论基础知识之如何形成高斯椭球
  • 【递归、搜索与回溯算法】专题三:穷举vs暴搜vs深搜vs回溯vs剪枝
  • 第十节第八部分:Lambda表达式、Lambda表达式的省略规则
  • YOLOV11网络图和数据增强
  • PotPlayer 4K 本地万能影音播放器
  • 基于厚度变化的分割点定位算法:以瓶口颈部为例