Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何以编程方式创建动态文本字段_Ios_Swift_Dynamic_Uitextfield_Xcode8 - Fatal编程技术网

Ios 如何以编程方式创建动态文本字段

Ios 如何以编程方式创建动态文本字段,ios,swift,dynamic,uitextfield,xcode8,Ios,Swift,Dynamic,Uitextfield,Xcode8,我想在用户单击视图的某个位置时添加一个动态文本字段。文本字段的大小应随着用户的键入而增加。目前我正在使用下面的代码 textfield = UITextField(frame: rect) textfield.backgroundColor = UIColor.clear textfield.layer.borderColor = UIColor.clear.cgColor textfield.autocorrectionT

我想在用户单击视图的某个位置时添加一个动态文本字段。文本字段的大小应随着用户的键入而增加。目前我正在使用下面的代码

textfield = UITextField(frame: rect)
            textfield.backgroundColor = UIColor.clear
            textfield.layer.borderColor = UIColor.clear.cgColor
            textfield.autocorrectionType = UITextAutocorrectionType.no
            textfield.textColor = UIColor.blue
            textfield.textAlignment = .right
            textfield.layer.borderWidth = 1.0
            textfield.delegate = self
            textfield.becomeFirstResponder()
但此文本字段不会自动增加高度和宽度。

您可以尝试

class ViewController: UIViewController , UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self 
    }

   func textViewDidChange(_ textView: UITextView) {

       let ff = textView.text! as NSString

       let rr = ff.size(withAttributes: [NSAttributedStringKey.font:UIFont(name: "Helvetica", size: 17)])

       textView.frame = CGRect(origin: textView.frame.origin, size: rr)
   }

}
//

阿拉伯文

func textViewDidChange(_ textView: UITextView) {

    let ff = textView.text! as NSString

   let rr = ff.size(withAttributes: [NSAttributedStringKey.font:UIFont(name: "Helvetica", size: 17)!])

    textView.frame = CGRect(origin:CGPoint(x: self.view.frame.width - rr.width, y: textView.frame.origin.y), size: rr)

 }

按照以下步骤操作

  • 在创建视图时单击视图即可创建UITextView 现在就开始吧
  • 设置委托并实现委托方法,如“textViewDidChange”或“UITextViewTextDidChange”通知,以便在文本更改时获取回调
  • 在回调方法内部,设置UITextView帧的宽度和高度 到其内容大小属性的宽度和高度!快乐编码
    如果需要更多帮助,请告诉我。

    您可能需要使用
    UITextView
    而不是
    UITextField
    我也尝试了UITextView,使用了与textview相同的方法。但它仍然没有增加大小请查看此链接说明。。我试过这个密码。func textViewDidChange(textView:UITextView){textView.frame=CGRect(原点:textView.frame.origin,大小:textView.contentSize)}但只有高度在增加,而不是宽度在增加。我需要两个UITextView的初始宽度是多少?请参见contentSize计算基于最初提供给UITextView的宽度。因此,宽度基本上是固定的,contentSize为您提供了所需的高度。要实现您想要的效果,您必须以编程方式计算文本宽度,然后必须有一个最大宽度值,因此,如果宽度增加了最大宽度,请修复UITextView宽度,并仅增加从contentSize属性获得的高度。应该有一个静态值和一个变量,文本在两个方向上展开没有意义应该有一个静态变量和一个变量,文本在两个方向上展开没有意义实际上,当用户键入文本时,文本视图的宽度必须增加。按“回车”键时,必须从下一行开始键入。这就是我想要的let rr=ff.size(带有属性:[NSAttributedStringKey.font:UIFont(名称:“Helvetica”,大小:17)!])。这行显示的是错误。请与我一起编译。再试一次,xcode,swift版本??