gpt4 book ai didi

ios - 如何在 rtf 文件中写入 NSAttributedString?

转载 作者:行者123 更新时间:2023-12-05 01:07:57 33 4
gpt4 key购买 nike

如何在 rtf 文件中写入 NSAttributedString?我找到了一个古老的答案How can I save the attributed string (text) into file (swift, cocoa)?我不太明白我到底需要什么,也许某处存在新的不同方式?

最佳答案

您可以使用 NSAttributedStringdata(from:) 方法将属性字符串转换为 rtf 数据。

extension NSAttributedString {
func rtf() throws -> Data {
try data(from: .init(location: 0, length: length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf,
.characterEncoding: String.Encoding.utf8])
}
}

let textView = UITextView()
textView.attributedText = .init(string: "abc",
attributes: [.font: UIFont(name: "Helvetica", size: 16)!])
do {
let rtfURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("RichTextDocument.rtf")
try textView.attributedText.rtf().write(to: rtfURL)
print("saved")
} catch {
print(error)
}

关于ios - 如何在 rtf 文件中写入 NSAttributedString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66667357/

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