Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Text View - Lines of text shifting up #23

Closed
jboo1212 opened this issue Dec 30, 2017 · 3 comments
Closed

Dynamic Text View - Lines of text shifting up #23

jboo1212 opened this issue Dec 30, 2017 · 3 comments

Comments

@jboo1212
Copy link

jboo1212 commented Dec 30, 2017

I have some sample code below where I try to mimic the dynamic text view calculation like your code. My question is how is the top inset addressed with respect to the text container? When I get to about the second line, my first line shifts up pretty much to very edge of the top frame of Text View. I want the text to stay center to the line height.

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textView.delegate = self
        self.textView.backgroundColor = .lightGray
    }
    
    func textViewDidChange(_ textView: UITextView) {
        self.calculateSize()
    }

    func calculateSize() {
        let fixedWidth = textView.frame.size.width
        textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
        var newFrame = textView.frame
        newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
        textView.frame = newFrame
    }
}

@KennethTsang
Copy link
Owner

Try to call textview.scrollRangeToVisible(NSMakeRange(-1, 0)) after height changing.

@jboo1212
Copy link
Author

Wow this does the trick! Is location = -1 defined by the text?

@KennethTsang
Copy link
Owner

The "-1" location seems not documented, but a little trick to scroll to bottom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants