ios - 我如何以编程方式创建动态文本字段

标签 ios swift dynamic uitextfield xcode8

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

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)

 }

enter image description here

关于ios - 我如何以编程方式创建动态文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358901/

相关文章:

ios - 使用 AFHTTPRequestOperation Manager 时响应失败

ios - 是否可以为不同大小的 UIImageView 提供不同的图像

ios - SCLAlertView 按钮

ios - 以编程方式添加 UIPageViewController

android - 在 Android 中显示表情符号

javascript - 添加/删除动态行 Javascript

iphone - 为布局约束添加优先级

ios - 无法快速从 NSURLConnection.sendSynchronousRequest 获取所有 HeaderFields

SwiftUI – @State 与 @Binding

java - 动态地将值添加到 JTable