Search code examples
iosswiftuitextviewcgfloat

Place the height of UITextView in CGFloat variable


I am resizing a UITextView based on its content. I would like to be able to then get the height of the textview and place it in a variable:

var heightFloat: CGFloat

override func viewDidLayoutSubviews() {

        super.viewDidLayoutSubviews()

        let contentSize = self.definitionTextView.sizeThatFits(self.definitionTextView.bounds.size)
        var frame = self.definitionTextView.frame
        frame.size.height = contentSize.height
        self.definitionTextView.frame = frame

        heightFloat = contentSize.height
        NSLog("heightFloat = %@", heightFloat)

    }

And then when the application is ran, the console simply returns:

heightFloat = (null)

How do I get height of the textview be stored within heightFloat?


Solution

  • Use CGRectGetHeight property.

    heightFloat = CGRectGetHeight(self.definitionTextView.frame)