当用户单击视图上的某个位置时,我想添加一个动态文本字段。文本字段应随用户键入而增加大小。目前,我正在使用以下代码
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()
但是此文本字段不会自动增加高度和宽度。
答案 0 :(得分:1)
您可以尝试
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)
}
答案 1 :(得分:0)
请按照以下步骤进行操作。
如果需要更多帮助,请告诉我。