iOS

[Swift/iOS] UITextView 세로 가운데 정렬 vertical center

guswlsdk 2022. 7. 26. 17:33
반응형

TextView의 높이를 변경함에 따라 본문이 작성되는 위치가 생각대로 되지 않는 경우가 있다.

 

UITextView에서 extension 코드를 작성하여 준다.

extension UITextView{
    func alignTextVerticallyInContainer() {
        var topCorrect = (self.bounds.size.height - self.contentSize.height * self.zoomScale) / 2
        topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
        self.contentInset.top = topCorrect
    }
}
@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.textView.alignTextVerticallyInContainer()
}

 

 

편------안

반응형