简体   繁体   中英

How to concatenate two UITextView

I'm trying to concatenate two UItextView and it work. They have different properties (for example different UIFont ) but in the final UITextView they have the same properties. How to fix this?

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

textViewSecond.text = textViewSecond.text + textViewFirst.text

for example : this makes your text bold from the 4th char to 7th

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

Try this:

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

textView2.attributedText = attributedString

In order to preserve both fonts and maybe other attributes (like text color) you must make use of NSAttributedString

        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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM