2

I am using this link to resize a textView dynamically. However, I implement it into a function and it return a height of 238. Using the heirarchy viewer I can see my textView is indeed 238, but there is a ton of empty space as 238 is way taller than needed. Can anyone figure out why textView.sizeThatFits: would give me this kind of error? Here is my code.

func heightOfTextFieldForMessage(message : String) -> CGFloat {
    let horizontalMargin = CGFloat(194)

    let textView = UITextView()
    textView.text = message
    textView.font = UIFont.systemFontOfSize(14)

    //This width is same as in heirachy viewer, horizontalMargin is a constant distance the textView is from edges of the screen.
    let fixedWidth = self.view.frame.width - horizontalMargin
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))


    let height = newSize.height
    print(height)
    return height
}

Is it possibly because my font is smaller than the default font? I tried changing the textView's font size before resizing but it seems like like the height does change based on what I set the font to.

The Text View bottom constraint is tied to the top of the icon so as you can see the textView is way taller than needed.

enter image description here

Community
  • 1
  • 1
NSGangster
  • 2,397
  • 12
  • 22
  • did you pass the trimmed text to heightOfTextFieldForMessage ? – ArunGJ May 18 '16 at 01:07
  • 1
    sizeThatFits is a calculation method ,it will change when the text attribute change – Wilson XJ May 18 '16 at 02:33
  • @WilsonXJ I know but the actual textView uses the same font/text attributes so shouldn't it return a height that is still good for my textView and not one that is way taller? – NSGangster May 23 '16 at 13:59
  • and @ArunGJ I just pass the string going into the textField. I'll post a screenshot so you guys can see the text. – NSGangster May 23 '16 at 14:17
  • @WilsonXJ I found the solution and it was a problem with the textView's font with calculation not matching the textViews font being used. I upvoted your comment but if you make an answer I'll accept it since your comment was the most valuable in helping me figure out my problem. – NSGangster May 23 '16 at 14:44
  • It's a pleasure to help you. – Wilson XJ May 23 '16 at 14:52
  • Either OP or @WilsonXJ should provide and accept an answer for this question. – d1str0 Sep 19 '16 at 16:33

0 Answers0