如何在UITextView中禁用选择功能? [英] How to disable select functionality in UITextView?

查看:832
本文介绍了如何在UITextView中禁用选择功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 基本UILabel:不允许
    滚动,并剪切文本到$ b屏幕上UILabel空间的$ b底部。

  2. UIScrollView中的巨型UILabel:UILabel将文本(垂直)放在巨型UILabel的中心,所以它经常在我的外面UIScrollView。

  3. UITextView:到目前为止,这种方法效果最好,因为它滚动并且不会剪切文本。我甚至可以将UITextView子类化为调整文本大小以适应文本视图,和将任何触摸事件传递给超级视图以检测点按和&挥笔但是,当用户点击并按住文本本身时,会出现文本选择界面。我对此界面不感兴趣,实际上它会分散用户的体验。我试图继承 canPerformAction:withSender:,但显然这个函数在点击事件之后被称为 - 而不是之前。

  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.

有没有人知道如何在UITextView中禁用文本选择界面,而不禁用滚动?

推荐答案

选择2号选项,但在上设置 numberOfLines 属性UILabel 0 。这会将行数设置为无限制并阻止文本的垂直居中。

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.

不要忘记设置 lineBreakMode 属性为 UILineBreakModeWordWrap ,以便您的文本不会在 UILabel 一侧运行。

Don't forget to set the lineBreakMode property to UILineBreakModeWordWrap so that your text doesn't run off the side of the UILabel.

这篇关于如何在UITextView中禁用选择功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆