こんちには、フリーのITエンジニアでWeb(PHP:Laravel)のバッグエンドをメインにフルリモートでお仕事させて頂きながら、個人開発でiOSアプリを作っているMoritaです。
最近は英語の勉強用に、個人開発で単語帳アプリを作成しています。
その中で、UITextViewの垂直方向のテキストをセンターにしたかったのですが、
デフォルト設定がなかったので調べてみました。
以下のサイトでコードで設定する方法がのっていたので大変助かりました!

How to center text vertically in an UITextView by using Swift3?
I create a custome text view called by: VerticallyCenteredTextViewclass VerticallyCenteredTextView: UITextView { overrid...
class VerticallyCenteredTextView: UITextView {
override var contentSize: CGSize {
didSet {
var topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
topCorrection = max(0, topCorrection)
contentInset = UIEdgeInsets(top: topCorrection, left: 0, bottom: 0, right: 0)
}
}
}
コメント