2016-06-26 73 views
0

我想创建一个可编辑文本的大图,但似乎有1个选项:使用一个小UITextField。多行可编辑的文本片段:可编辑的UILabel?

我知道UILabels可能大而宽,但我不知道如何制作可编辑的UILabel。

我用UILabel属性和.layer方法进行了实验,但没有任何东西看起来真的起作用。有人建议我应该做什么?

总之,我正在寻找一个多行可编辑的文本片段。

回答

2

UITextView获胜!

UITextViews允许对文本进行多行处理,如果您使用UITextViewDelegate,它可以提供允许在单击textView时等特定事物的方法。

使用UITextView,您可以提供特定数量的行(如果您只需要3个,您可以指定它),并且还可以提供超链接(如果需要)。

下面是一个例子,我有(稍有变化),以示对雅为例...

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) 
+0

你是最棒的!两颗金星!其中一个为正确答案,其次为你的热情。干杯! –

+0

谢谢先生!随时在这里发布更多的问题/评论,我会帮助!祝你的编码冒险! – impression7vx