Wordwrap label in Swift
12,637
Remove height constraint from label in Xib (If you have set constraint for height)
and Use this
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 0
label.text = textfield.text
Author by
Emre Önder
By day: I'm working as a software developer By Night: I'm also a software developer, mostly in iOS.
Updated on June 04, 2022Comments
-
Emre Önder almost 2 years
I searched for this problem but the solutions i found didn't work for me. I have a textfield. When user writes something to textfield, it writes it to the label. This label is on an imageview, when label reaches the imageview limits. I want that label wordwrap and go to second line. Now it is like this but doesn't work.
@IBAction func textchanged(_ sender: UITextField) { if(mylabel.frame.minX < imageview.frame.minX && mylabel.frame.maxX > imageview.frame.maxX) { mylabel.frame = CGRect(x: mylabel.frame.origin.x, y: mylabel.frame.origin.y, width: mylabel.frame.width, height: mylabel.frame.height * 2 ) mylabel.numberOfLines = 2 mylabel.lineBreakMode = NSLineBreakMode.byWordWrapping mylabel.sizeToFit() } mylabel.numberOfLines = lines mylabel.text = textfield.text mylabel.sizeToFit() }
-
Emre Önder over 7 yearsIt gives this error: ` Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[instayaz2.ViewController textFieldShouldEndEditing:]: unrecognized selector sent to instance 0x7fed1640a7d0'`
-
Emre Önder over 7 yearsDelegate is like this; textfield.delegate = self textfield.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(:)), for: UIControlEvents.editingChanged) panRecognizer.addTarget(self, action: #selector(ViewController.draggedView(:)))
-
Alex Thomas over 7 yearstry textfield.delegate = self
-
Emre Önder over 7 yearsIt doesn't have a height constraint.