Ryan Ryan - 3 months ago 23x
Swift Question

Editable UILabel? iOS [SWIFT]

I am trying to create a large plot of editable text but there seems to be 1 option: (1) 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

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

Thank you for all help!


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)!,
            NSParagraphStyleAttributeName: style])
        attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
        textBox.attributedText = attributedText