如何禁用 UITextView 中的选择功能?

发布于 2024-09-12 15:14:50 字数 790 浏览 3 评论 0原文

我想在我的 iPhone 应用程序中创建一个文本显示区域(UILabel 或 UITextView),它 (1) 允许滚动,(2) 不允许选择。我尝试了以下技术但没有成功:

  1. Basic UILabel:不允许 滚动,并将文本剪切到 屏幕上 UILabel 空间的底部。
  2. UIScrollView 中的巨型 UILabel:UILabel 始终将文本(垂直)放置在巨型 UILabel 的中心,因此它通常位于我的 UIScrollView 之外。
  3. 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:

  1. Basic UILabel: Didn't allow
    scrolling, and clipped text to the
    bottom of UILabel space on screen.
  2. 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.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-09-19 15:14:50

采用选项 2,但将 UILabel 上的 numberOfLines 属性设置为 0。这会将行数设置为“无限制”并防止文本垂直居中。

不要忘记将 lineBreakMode 属性设置为 UILineBreakModeWordWrap,这样您的文本就不会超出 UILabel 的一侧。

Take option number 2, but set the numberOfLines property on your UILabel to 0. 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 to UILineBreakModeWordWrap so that your text doesn't run off the side of the UILabel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文