gpt4 book ai didi

cocoa - 如何将文本附加到 NSTextView 实例的开头?

转载 作者:行者123 更新时间:2023-12-03 17:46:53 24 4
gpt4 key购买 nike

在我的 macOS 应用程序中,有一个 NSTextView 显示一些日志。生成日志时,我使用 NSTextView.textStorage.append(NSAttributedString) 将其添加到带有换行符的 View 末尾。

(1) 我不确定如何将日志添加到 View 的开头。

(2) 我觉得每个日志应该是一个单独的元素,而不是带有换行符的字符串,我应该使用其他方法来构建此日志 View 吗?

class LogView: NSTextView {

func log(text: String) {
DispatchQueue.main.async {
self.textStorage?.append(NSAttributedString(string: "\(text)\n", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white]))
}
}
}

最佳答案

textStorage继承自NSAttributedString,如果有append,则还有insert(at:)

class LogView: NSTextView {

func log(text: String) {
DispatchQueue.main.async {
self.textStorage?.insert(NSAttributedString(string: "\(text)\n", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white]), at: 0)
}
}
}

关于cocoa - 如何将文本附加到 NSTextView 实例的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com