I am writing from scratch growing UITextView
in my swift
app.
I put a textView
on the view
like this:
it is right above the keyboard.
The textView
has constraints attached to the view
: leading
, bottom
, top
and trailing
, all equals = 4
.
The view
has the following constraints:
trailing
, leading
, bottom
, top
and height
Height
is an outlet in my code. I'm checking how many lines are in the textView
and based on that I'm modifying height
:
func textViewDidChange(textView: UITextView) { //Handle the text changes here
switch(textView.numberOfLines()) {
case 1:
heightConstraint.constant = 38
break
case 2:
heightConstraint.constant = 50
break
case 3:
heightConstraint.constant = 70
break
case 4:
heightConstraint.constant = 90
break
default:
heightConstraint.constant = 90
break
}
}
The number of lines above is calculated by this extension:
extension UITextView{
func numberOfLines() -> Int{
if let fontUnwrapped = self.font{
return Int(self.contentSize.height / fontUnwrapped.lineHeight)
}
return 0
}
}
The initial height of the textView
is 38
.
The initial font size in the textView
is 15
.
Now, it works nice, when user starts typing new line, but the textView
is not set within full bounds of the view. I mean by that the fact, that it looks like this:
and it should look like this:
Why there is this extra white space being added and how can I get rid of it?
Currently when new line appears there's this white space, but when user scrolls the textView
to center the text and get rid of the white space - it is gone forever, user is not able to scroll it up again so the white line is there. So for me it looks like some problem with refreshing content, but maybe you know better - can you give me some hints?