如何连接两个UITextView

时间:2015-06-05 14:20:54

标签: ios swift uitextview

我正在尝试连接两个UItextView并且它可以工作。 它们具有不同的属性(例如,不同的UIFont)但在最终的UITextView中它们具有相同的属性。如何解决这个问题?

textViewFirst!.text = "\n Example"
textViewFirst!.font = UIFont(name: "Helvetica Neue", size: 10);

textViewSecond.text = textViewSecond.text + textViewFirst.text

3 个答案:

答案 0 :(得分:0)

例如:这使您的文本从第4个字符加粗到第7个

let myFullString:String = textViewSecond.text + textViewFirst.text as String

var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: myFullString)

attributedText.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(14)], range: NSRange(location: 3, length: 3))

textViewSecond.attributedText = attributedText

答案 1 :(得分:0)

试试这个:

var attributedString = NSMutableAttributedString(string: textView2.text, attributes: [NSFontAttributeName : textView2.font])
attributedString.appendAttributedString(NSAttributedString(string: textView1.text, attributes: [NSFontAttributeName : textView1.font]))

textView2.attributedText = attributedString

为了保留字体和其他属性(如文字颜色),您必须使用NSAttributedString

答案 2 :(得分:0)

        let font = UIFont(name: "Helvetica Neue", size: 10.0) ?? UIFont.systemFontOfSize(18.0)
        let textFont = [NSFontAttributeName:font]

        // Create a string that will be our paragraph
        let para1 = NSMutableAttributedString()
        let para2 = NSMutableAttributedString()

        // Create locally formatted strings
        let attrString1 = NSAttributedString(string: "Hello ", attributes:textFont)
        let attrString2 = NSAttributedString(string: "World ", attributes:textFont)


        // Add locally formatted strings to paragraph
        para1.appendAttributedString(attrString1)
        para2.appendAttributedString(attrString2)


        // Define paragraph styling
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.firstLineHeadIndent = 15.0
        paraStyle.paragraphSpacingBefore = 10.0

        // Apply paragraph styles to paragraph
        para1.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: para1.length))
        para2.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: para1.length))

        // Create UITextView
        let view1 = UITextView(frame: CGRect(x: 0, y: 20, width: CGRectGetWidth(self.view.frame), height: 100))
        let view2 = UITextView(frame: CGRect(x: 0, y: 100, width: CGRectGetWidth(self.view.frame), height: 100))
         let view3 = UITextView(frame: CGRect(x: 0, y: 200, width: CGRectGetWidth(self.view.frame), height: 100))

        // Add string to UITextView
        view1.attributedText = para1
        view2.attributedText = para2

        var attributedString = NSMutableAttributedString(string: view1.text, attributes: [NSFontAttributeName : view1.font])
        attributedString.appendAttributedString(NSAttributedString(string: view2.text, attributes: [NSFontAttributeName : view2.font]))

        view3.attributedText = attributedString

        // Add UITextView to main view
        self.view.addSubview(view1)
        self.view.addSubview(view2)
        self.view.addSubview(view3)