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

【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.dtcms.com/a/210506.html

相关文章:

  • 毕业论文格式(Word)
  • python 自动生成不同行高的word
  • 攻防世界——Web题 unseping 反序列化绕过
  • 计算机视觉与深度学习 | 基于 YOLOv8 + BeautyGAN + CodeFormer + Face Parsing 实现简单的人脸美颜
  • Spring Security探索与应用
  • 如何进行CAN一致性测试
  • 从稳定到卓越:服务器部署后的四大核心运维策略
  • 传奇各种怪物一览/图像/爆率/产出/刷新地/刷新时间/刷怪时间
  • LeetCode 2942.查找包含给定字符的单词:使用库函数完成
  • vs2022 Qt Visual Studio Tools插件设置
  • 人工智能100问☞第31问:如何评估一个AI模型的性能?
  • IPC进程间通信详解
  • 索引下探(Index Condition Pushdown,简称ICP)
  • MCP与AI模型的多语言支持:让人工智能更懂世界
  • 数据库6——综合实验-水果商店进阶一
  • Axure酒店管理系统原型
  • Python入门手册:Python中的数据结构类型
  • Gartner《Optimize GenAI Strategy for 4 Key ConsumerMindsets》学习心得
  • 力扣:《螺旋矩阵》系列题目
  • 豪越科技:消防应急装备智能仓储管理新变革
  • 深入理解设计模式:工厂模式、单例模式
  • 解析 Flask 上下文机制:请求上下文、应用上下文
  • HTTP协议初认识、速了解
  • 机器学习中的多GPU训练模式
  • WebXR 虚拟现实开发
  • Python爬虫(37)Python爬虫深度实践:Splash渲染引擎与BrowserMob Proxy网络监控协同作战
  • 使用 Go 语言实现完整且轻量级高性能的 MQTT Broker
  • vue3使用七牛云上传文件
  • MySQL主从同步原理
  • 快速失败(fail-fast)和安全失败(fail-safe)的区别