Multi-line editable piece of text: editable UILabel?

房东的猫 提交于 2019-12-25 01:39:36

问题


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:


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)


来源:https://stackoverflow.com/questions/38035556/multi-line-editable-piece-of-text-editable-uilabel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!