UITextView 中光标的奇怪定位 [英] weird positioning of the cursor in the UITextView
问题描述
我正在我的 swift
应用程序中从头开始编写不断增长的 UITextView
.我在 view
上放了一个 textView
像这样:
I am writing from scratch growing UITextView
in my swift
app.
I put a textView
on the view
like this:
它就在键盘的正上方.
textView
具有附加到 view
的约束:leading
、bottom
、top
和 trailing
,都等于 4
.
The textView
has constraints attached to the view
: leading
, bottom
, top
and trailing
, all equals = 4
.
view
具有以下约束:
trailing
、leading
、bottom
、top
和 height
Height
是我代码中的一个出口.我正在检查 textView
中有多少行,并基于此修改 height
:
Height
is an outlet in my code. I'm checking how many lines are in the textView
and based on that I'm modifying height
:
func textViewDidChange(textView: UITextView) { //Handle the text changes here
switch(textView.numberOfLines()) {
case 1:
heightConstraint.constant = 38
break
case 2:
heightConstraint.constant = 50
break
case 3:
heightConstraint.constant = 70
break
case 4:
heightConstraint.constant = 90
break
default:
heightConstraint.constant = 90
break
}
}
上面的行数是通过这个扩展来计算的:
The number of lines above is calculated by this extension:
extension UITextView{
func numberOfLines() -> Int{
if let fontUnwrapped = self.font{
return Int(self.contentSize.height / fontUnwrapped.lineHeight)
}
return 0
}
}
textView
的初始高度为 38
.textView
中的初始字体大小为 15
.
The initial height of the textView
is 38
.
The initial font size in the textView
is 15
.
现在,当用户开始输入新行时,它运行良好,但 textView
未设置在视图的完整范围内.我的意思是,它看起来像这样:
Now, it works nice, when user starts typing new line, but the textView
is not set within full bounds of the view. I mean by that the fact, that it looks like this:
它应该是这样的:
为什么要添加这个额外的空白,我该如何摆脱它?
Why there is this extra white space being added and how can I get rid of it?
目前当出现新行时有这个空白,但是当用户滚动 textView
以将文本居中并摆脱空白时 - 它永远消失了,用户无法滚动它再次向上,所以白线在那里.所以对我来说,刷新内容似乎有些问题,但也许你更清楚 - 你能给我一些提示吗?
Currently when new line appears there's this white space, but when user scrolls the textView
to center the text and get rid of the white space - it is gone forever, user is not able to scroll it up again so the white line is there. So for me it looks like some problem with refreshing content, but maybe you know better - can you give me some hints?
推荐答案
这是我在我正在开发的一个应用程序的评论部分中使用的一种有点不同的方法.这与 Facebook Messenger iOS 应用程序的输入字段非常相似.更改了插座名称以与您问题中的名称相匹配.
Here is a bit different approach I use in the comment section of one of the apps I'm developing. This works very similar to Facebook Messenger iOS app's input field. Changed outlet names to match with the ones in your question.
//Height constraint outlet of view which contains textView.
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var textView: UITextView!
//Maximum number of lines to grow textView before enabling scrolling.
let maxTextViewLines = 5
//Minimum height for textViewContainer (when there is no text etc.)
let minTextViewContainerHeight = 40
func textViewDidChange(textView: UITextView) {
let textViewVerticalInset = textView.textContainerInset.bottom + textView.textContainerInset.top
let maxHeight = ((textView.font?.lineHeight)! * maxTextViewLines) + textViewVerticalInset
let sizeThatFits = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max))
if sizeThatFits.height < minTextViewContainerHeight {
heightConstraint.constant = minTextViewContainerHeight
textView.scrollEnabled = false
} else if sizeThatFits.height < maxHeight {
heightConstraint.constant = sizeThatFits.height
textView.scrollEnabled = false
} else {
heightConstraint.constant = maxHeight
textView.scrollEnabled = true
}
}
func textViewDidEndEditing(textView: UITextView) {
textView.text = ""
heightConstraint.constant = minTextViewContainerHeight
textView.scrollEnabled = false
}
这篇关于UITextView 中光标的奇怪定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!