问题
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?
回答1:
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
.
来源:https://stackoverflow.com/questions/3397857/how-to-disable-select-functionality-in-uitextview