更新/替换textview中的文本

时间:2017-11-21 19:23:03

标签: swift uitextfield uitextview nsattributedstring

我在第一个视图控制器中有一个TextField,用户输入他的名字。在第二个视图控制器中,我在textview中有一个表单,用户在其中输入要替换特定位置中特定单词的详细信息。 我无法从各种搜索中找到正确的指导,我只能找到格式化文本/颜色等。我的目标是用用户填充的数据替换现有文本。

     import UIKit

class SandyaViewController: UIViewController, UITextFieldDelegate {
   func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "SViewController" {
            if let nextVC = segue.destination  as? SPViewController {
            nextVC.yourName = self.nameTextField.text!
            nextVC.address = self.addressTextField.text!
            }
        }
    }
}    
    class SPViewController: UIViewController {
    var yourName: String!
    var address: String!

    @IBOutlet weak var formTextView: UITextView!

    myTextView.text = "This is to certify , that I yourname  residing at home address....."

   let attributedString = NSMutableAttributedString(string: "This is to certify , that I your name  residing at home address .........\n")

let attributes1: [NSAttributedStringKey : Any] = [
   NSAttributedStringKey.foregroundColor: UIColor(red: 153/255, green: 51/255, blue: 255/255, alpha: 1.0),
   NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 15)!,
   NSAttributedStringKey.backgroundColor: UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1.0)
]
attributedString.addAttributes(attributes1, range: NSMakeRange(28, 9))

let attributes3: [NSAttributedStringKey : Any] = [
   NSAttributedStringKey.foregroundColor: UIColor(red: 230/255, green: 0/255, blue: 0/255, alpha: 1.0),
   NSAttributedStringKey.backgroundColor: UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1.0)
]
attributedString.addAttributes(attributes3, range: NSMakeRange(51, 12))

这里我希望将attributes1值替换为yourName,将attributes3值替换为address myTextView.text ="这是为了证明,我归属于家庭属性3 .......#34; 我认为我已经详细说明了 感谢

0 个答案:

没有答案