无法动态正确获取 uitextview 高度
我在获取 uitextview 的正确大小时遇到问题。在我的应用程序中,我从 webservice 获取数据,并根据 webservice 的数据设置 uitextview 的高度。但是只要前 35 个字母(我的 textview 的框架)中有空格是 (20,0,255,2000))。那么计算出的 hjeight 总是比正确的高度少一行,甚至 uitext 视图也开始在新行中的空格之后打印该行(可能是单词很大,这就是为什么??)。
所以我得到了错误的文本视图高度。
文本视图看起来像这样:
firstword 空间空间空间空间 空间空间空间空间 空间空间空间空间 空间空间空间空间 spacespacesspacesspaces
当我输入没有空格的文本时,它会给我正确的高度,假设数据是“tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt” 然后在上面的场景中 Cgsize 将为我生成正确的结果。
在这里我没有使用 UILabel 是因为我想要链接检测,这就是原因。 如果有人有任何其他建议,我们非常欢迎。
问候 姆鲁根
I am having issue in getting the correct size of uitextview.In my application i am fetching the data from webservice and i set the height of uitextview as per the data from webservice .But whenever there is space in first 35 letters(frame of my textview is (20,0,255,2000)). then the hjeight calculated is always one line less then correct height and even the uitext view starts to print the line after space in the new line (may be word is big thats why ??).
So i am getting the wrong text view height.
The text view apears somwethibng like this :
firstword
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces
When i enter text which is say without spaces it gives me correct height suppose say the data is "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
Then in the above scenerio Cgsize will generate proper result for me .
Here i am not using UILabel is because i want link detection thats why .
If anyone has anyother suggestions it is highly welcomed.
Regards
Mrugen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 NSString 的 sizeWithFont 方法并不真正适用于文本视图。它非常适合 UILabel,但文本视图似乎在文本周围有额外的填充。如果你使用这种方法,你最终得到的尺寸会有点短。
UITextView 是 UIScrollView 的子类。设置 textview 的 text 属性后,其 contentSize 属性会自动更新以适合文本。因此,如果您想设置文本视图的高度以适合其文本,只需执行以下操作:
这将设置您的 textView 的高度,使其包含文本。
Using NSString's sizeWithFont methods doesn't really work for a text view. It works great for a UILabel, but text views seem to have extra padding around the text. If you use that method, you end up with a size that's a little short.
UITextView is a subclass of UIScrollView. After you set the textview's text property, its contentSize property is automatically updated to fit the text. So, if you want to set the text view's height to fit its text just do:
That will set your textView's hight so it contains the text.
您可以使用此函数测量文本的高度,并根据返回的高度设置框架。
你这样使用它:
然后将它传递给文本视图:
祝你好运
沙尼
You can measure the height of the text with this function and set your frame according to the returned height.
you use it like that:
And then you pass it to the text view:
Good luck
shani
从 iOS7 开始,您现在应该使用:
As of iOS7 you should now use:
你的问题的答案是
为 UITextView 创建一个实例,在我的例子中,我创建为 txtView,然后向其中添加文本并在控制台中打印出内容高度的高度。
NSLog(@"TextView高度为:%d",txtView.contentSize.height)
The answer for your question is
Create an instance for UITextView,In my case I have created as txtView and then add text to it and print out the height in content height in console.
NSLog(@"TextView Height is :%d",txtView.contentSize.height)
获取任何
textField
的估计高度和宽度:(Swift 4)它以 TextField 的文本作为参数。
Get the estimated
Height
andWidth
of anytextField
: (Swift 4)It take the text of TextField as argument.
在 Swift 4 中尝试过,
textView.intrinsicContentSize.height
Tried in Swift 4,
textView.intrinsicContentSize.height