如何禁用 UITextView 中的选择功能?
我想在我的 iPhone 应用程序中创建一个文本显示区域(UILabel 或 UITextView),它 (1) 允许滚动,(2) 不允许选择。我尝试了以下技术但没有成功:
- Basic UILabel:不允许 滚动,并将文本剪切到 屏幕上 UILabel 空间的底部。
- UIScrollView 中的巨型 UILabel:UILabel 始终将文本(垂直)放置在巨型 UILabel 的中心,因此它通常位于我的 UIScrollView 之外。
- UITextView:到目前为止,这种方法效果最好,因为它可以滚动并且不会剪切文本。我什至可以子类化 UITextView 以启用调整文本大小以适合文本视图,和 将任何触摸事件传递到超级视图以检测点击和点击滑动。 但是,当用户点击并按住文本本身时,会出现文本选择界面。我对这个界面不感兴趣,它实际上分散了用户的体验。我尝试将
canPerformAction:withSender:
进行子类化,但显然此函数是在点击事件之后调用的,而不是之前。
有谁知道如何禁用 UITextView 中的文本选择界面,而不同时禁用滚动?
I want to create a text display area (either UILabel or UITextView) in my iPhone app which (1) allows scrolling and (2) does not allow selection. I have tried the following techniques unsuccessfully:
- Basic UILabel: Didn't allow
scrolling, and clipped text to the
bottom of UILabel space on screen. - Giant UILabel within a UIScrollView: the UILabel kept placing the text (vertically) at the center of the giant UILabel, so it often was outside of my UIScrollView.
- UITextView: So far this approach has worked the best, because it scrolls and does not clip the text. I can even subclass UITextView to enable resizing the text to fit within the textview, and passing any touch events to the superview to detect taps & swipes. But, when the user taps and holds on the text itself, the text selection interface appears. I am not interested in this interface and it actually is distracting from the user's experience. I have tried to subclass
canPerformAction:withSender:
, but apparently this function is called after the tap event--not before it.
Does anyone know how to disable the text selection interface in UITextView, without also disabling scrolling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
采用选项 2,但将
UILabel
上的numberOfLines
属性设置为0
。这会将行数设置为“无限制”并防止文本垂直居中。不要忘记将
lineBreakMode
属性设置为UILineBreakModeWordWrap
,这样您的文本就不会超出UILabel
的一侧。Take option number 2, but set the
numberOfLines
property on yourUILabel
to0
. This will set the number of lines to 'unlimited' and prevent the vertical centring of the text.Don't forget to set the
lineBreakMode
property toUILineBreakModeWordWrap
so that your text doesn't run off the side of theUILabel
.