swift - 如何在 swift 代码中的 Ui TextView 中上传 RTF 文件

标签 swift xcode6 uitextview rtf

如何在 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/

相关文章:

ios - CLLocationManager 有时会返回 (0, 0) 坐标

swift - 如何在 Xcode 中导入和使用 CreateML 或 CreateMLUI

ios - 动态设置Scrollview的Contentsize不起作用

ios - 类型不符合未定义的协议(protocol)

ios - Xcode 6 符号不工作

ios - 苹果 LLVM 6.0 错误

iphone - UITableViewCell sizeToFit 中的 UITextView 未按预期工作

ios - Swift 字符串中的换行符和/或回车符

Swift:扩展 print() 函数的功能

ios - UITextContainerView 不调整大小作为他的 UITextView superview,仅当旋转角度大于 45°