gpt4 book ai didi

ios - 在 Swift 2 中将 rtf 文件加载到 UITextView

转载 作者:可可西里 更新时间:2023-11-01 03:02:23 24 4
gpt4 key购买 nike

有人可以帮助我使用 Swift 2 将 rtf 文本加载到 UITextView 中吗?我得到的答案是旧的和过时的。文本是关于如何玩我在应用程序中编写的游戏的说明。到目前为止,我所能做的就是将所有 rtf 文本复制并粘贴到占位符框中。这适用于模拟器中的 iPhone,但在 iPad 模拟器或 iPhone 6 Plus 中尝试时,出现双垂直滚动条。看起来很乱。

我现在也有同一个文件的纯文本,所以我们也可以试试。

最佳答案

swift 3

        if let rtfPath = Bundle.main.url(forResource: "SomeTextFile", withExtension: "rtf") {
do {
let attributedStringWithRtf:NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
self.textView.attributedText = attributedStringWithRtf
} catch let error {
print("Got an error \(error)")
}
}

swift 4 和 5

    if let rtfPath = Bundle.main.url(forResource: "someRTFFile", withExtension: "rtf") {
do {
let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
self.textView.attributedText = attributedStringWithRtf
} catch let error {
print("Got an error \(error)")
}
}

关于ios - 在 Swift 2 中将 rtf 文件加载到 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35691851/

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