如何上传RTF文件在Ui文本视图中的swift代码

时间:2015-04-16 15:23:13

标签: swift xcode6 uitextview rtf

如何在UITextView中加载RTF文件?

2 个答案:

答案 0 :(得分:3)

假设您在应用中添加了一个名为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)

编辑:故事板版本

根据要求提供完整代码。这假设您已从故事板连接了一个插座。

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
        }


    }

}

注意:文字不可编辑,但仍可选择。

答案 1 :(得分:2)

使用iOS9

上面的[HttpPost] public ActionResult Update(CustomerViewModel model) { var customer = yourDbContext.Customers.FirstOrDefault(s=>s.Id==model.Id); if(customer!=null) { // Updating only fields which are supposed to be updated from the view. customer.FirstName = model.FirstName; customer.LastName = model.LastName; yourDbContext.Entry(customer).State = EntityState.Modified; yourDbContext.SaveChanges(); return RedirectToAction("UpdatedSuccessfully"); } return View("NotFound"); } 字符串会出错。您可以尝试以下代码:

NSAttributed