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

【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.lryc.cn/news/2386501.html

相关文章:

  • 超越OpenAI CodeX的软件工程智能体:Jules
  • Qt实战教程:设计并实现一个结构清晰、功能完整的桌面应用
  • 轻量化MEC终端 特点
  • NIST提出新型安全指标:识别潜在被利用漏洞
  • List介绍
  • 正则表达式全解:一文学会正则表达式【附在线正则表达式练习网站】
  • Nginx-详解(二)
  • 解决 IntelliJ IDEA 配置文件中文被转义问题
  • MCP、MCPHub、A2A、AG-UI概述
  • 计算机视觉与深度学习 | Python实现CEEMDAN-ISOS-VMD-GRU-ARIMA时间序列预测(完整源码和数据)
  • [Linux]磁盘分区及swap交换空间
  • JAVA面向对象——对象和类的基本语法
  • Linux常见指令合集+知识点
  • nginx 基于IP和用户的访问
  • 【Linux】系统程序−进度条
  • Axure应用交互设计:动态面板嵌套实现超强体验感菜单表头
  • Linux(6)——第一个小程序(进度条)
  • CentOS:搭建国内软件repository,以实现自动yum网络安装
  • [Git] 认识 Git 的三大区域 文件的修改和提交
  • RISC-V 开发板 MUSE Pi Pro USB 测试(3.0 U盘,2.0 UVC摄像头)
  • 【520 特辑】用 HTML/CSS/JavaScript 打造浪漫炫酷的表白网页
  • 小米2025年校招笔试真题手撕(二)
  • 弱网服务器群到底有什么用
  • 部署Gitlab-CE with Docker私有云环境
  • 拉普拉斯高斯(LoG)滤波器掩模的注意事项
  • 铠大师:让用户畅享多元应用,助力鸿蒙生态发展
  • RocketMQ核心特性与最佳实践
  • springboot配置redis lettuce连接池,以及连接池参数解释
  • 基于aspnet,微信小程序,mysql数据库,在线微信小程序汽车故障预约系统
  • 如何使用AI搭建WordPress网站