如何在 UITextView 中加载 RTF 文件?
最佳答案
假设您在您的应用中包含一个名为 rtfdoc.rtf
的文件,这将使您开始:
let rtf = NSBundle.mainBundle().URLForResource("rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil)
let attributedString = NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
let textView = UITextView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
textView.attributedText = attributedString
self.view.addSubview(textView)
编辑: Storyboard版本
要求的完整代码。这假设您已经连接了 Storyboard的 socket 。
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
if let rtf = NSBundle.mainBundle().URLForResource("rtfdoc", withExtension: "rtf", subdirectory: nil, localization: nil) {
let attributedString = NSAttributedString(fileURL: rtf, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
textView.attributedText = attributedString
textView.editable = false
}
}
}
注意:文本不可编辑,但仍可选择。
关于swift - 如何在 swift 代码中的 Ui TextView 中上传 RTF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29679229/