根据行数在文本视图中启用滚动
如果行数超过 5,我想在文本视图中启用滚动,否则不应有任何滚动。这可能吗?如何实现?
I want to enable scrolling in the textview if the number of lines exceeds 5 otherwise there should not be any scrolling. Is that possible and how to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UITextView 继承自 UIScrollView,它有一个名为
scrollEnabled
的属性,您可以添加将您的类注册为 UITextViewDelegate 并实现方法
- (void)textViewDidChange:(UITextView *)textView
然后从
textView
对象获取text
属性,并检查有多少换行符/回车符。如果超过 5 个,则将scrollEnabled
设置为YES
更新:
查看 NSString UIKit Additions,这个类中有一些方法可以让你获取
NSString
的CGSize
,特别是sizeWithFont:constrainedToSize:lineBreakMode:
使用此功能,您应该能够在
CGSize
后启用滚动code> 达到等于或大于由uifont.lineHeight*5 计算的 5 行文本的高度
UITextView inherits from UIScrollView which has a property called
scrollEnabled
You can add register your class as the UITextViewDelegate and implement the method
- (void)textViewDidChange:(UITextView *)textView
Then from the
textView
object get thetext
property, and check to see how many newlines/carriage returns there are. If there are more than 5, then setscrollEnabled
toYES
UPDATE:
Take a look at NSString UIKit Additions, there are some methods in this class that allow you to get the
CGSize
of yourNSString
, specificallysizeWithFont:constrainedToSize:lineBreakMode:
Using this you should be able to enable scrolling once the
CGSize
reaches a height equivalent or greater than 5 lines of text calculated byuifont.lineHeight*5
尝试使用此代码:
Try to use this code: