0

I am trying to create a large plot of editable text but there seems to be 1 option: using a small UITextField.

I know UILabels can be big and wide but I do not know how to make an editable UILabel.

I experimented with UILabel properties and the .layer method but nothing seems to be really working. Anybody have a recommendation as to what I should do?

To summarize, I am looking for a multi-line editable piece of text.

1 Answer 1

3

UITextView for the win!!

UITextViews allow for multiple line manipulation of texts and if you use the UITextViewDelegate, it can provide for methods that allow specific things when the textView is clicked on, etc...!

With a UITextView you can provide a specific amount of lines (if you only want 3, you can specify it) and also provide hyperlinks, if need be.

Here is an example I have (changed a little) to show an example for ya...

let textBox:UITextView = UITextView(frame: CGRect(x: firstBox.frame.width*0, y: firstBox.frame.height*0.375, width: firstBox.frame.width*1, height: firstBox.frame.height*0.5))
        textBox.backgroundColor = UIColor.clearColor()

        let websiteName = "http://stackoverflow.com/posts/38035564"
        textBox.text = "SO is an awesome coding site! Please visit\n\(websiteName)"

        //No need to set number of lines, it will auto set to as many as needed!
        textBox.editable = false
        textBox.selectable = true

        //Register the hyperlink
        textBox.dataDetectorTypes = UIDataDetectorTypes.All
        textBox.textColor = UIColor.grayColor()

        //Change only the hyperlink part
        let textRange = NSMakeRange(textBox.text.characters.count-websiteName.characters.count, websiteName.characters.count)
        let style = NSMutableParagraphStyle()
        style.alignment = NSTextAlignment.Center

        let attributedText = NSMutableAttributedString(string: textBox.text, attributes: [NSFontAttributeName:UIFont(
            name: (textBox.font?.fontName)!,
            size:13/15*fontSize)!,
            NSParagraphStyleAttributeName: style])
        attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
        textBox.attributedText = attributedText
        firstBox.addSubview(textBox)
2
  • You are the best! Two Gold Stars! One for the correct answer, second for your enthusiasm. Cheers! Jun 26, 2016 at 5:55
  • Thank you sir! Feel free to post more question/comment here and I will be on to help! Good luck on your coding adventures! Jun 26, 2016 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.