textview 中的文本不是垂直居中

Bra*_*'in 3 uitextview swift

我正在尝试创建一个文本视图,其高度跟随其中的行数。我一直在研究约束,但在这里我有一个奇怪的问题:

  • 一开始一切都是我想要的中心

在此处输入图片说明

  • 当有中断时,文本不再垂直居中并进入左上角

在此处输入图片说明

  • 当你删除字符然后回到上一行时,文本仍然垂直居中

在此处输入图片说明

这是代码

func textViewDidChange(textView: UITextView) { //Handle the text changes here
    if(textView.text != ""){
        self.animateSendButton(true)
    }else{
        self.animateSendButton(false)
    }//the textView parameter is the textView where text was changed

    heightTextfieldConstraint.constant = self.textField.contentSize.height + 2
    textField.contentInset.top = 1
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会有这 3 种不同的情况,你有什么解决办法吗?

Dio*_*nes 5

您可以尝试在顶部、机器人、左侧和右侧添加一个插图,即使出现第二行,也应该使文本居中。

textView.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
Run Code Online (Sandbox Code Playgroud)