UITextView

These days I am working on UITextView, before we want to make copy/paste available we use UILabel instead of UITextView. UILabel is a good way to show texts, unless you want it to be editable, because UILabel is much faster than UITextView. Now I will summize some points of UITextView, welcome everybody to add more.

IPadding

The first thing that confused me was the padding of UITextView, there are always paddings around texts of UITextView, the size of paddings is related to font size. In my app, I want to remove these paddings, and here are two ways. UITextView is inherited from UIScrollView, so we can use the property contentInset.

1
textview.contentInset = UIEdgeInsetsMake(-8,-8,0,0);

use this code, we can remove left and top paddings, maybe you will change the numbers according to you font. In my experiments, ‘-8’ is good for most situations. But I haven’t find ways to remove right and bottom paddings, if you know, plean leave comment below.

The other way is ,we can add TextView as a subView of an instance of UIView, which we set clipToBounds = YES.

1
2
3
4
5
CGFloat padding = 8;
CGRect newFrame = CGRectMake(-padding, -padding, oldFrame.bounds.size.width + 2*padding, oldFrame.bounds.size.height + 2*padding);
textView.frame = newFrame;
UIView* container = [[UIView alloc] initWithFrame:oldFrame];
[container addSubview:textView];

###Menu
When using UITextView, we usually want to use menu for copy/paste/select, the simplest way to customize the menu is override the canPerformAction function, returning NO for unWelcomed menu item.
Of course, we can do more about the menu, this menu is a Singleton instance of UIMenuController, you can add more menu item by set the property menuItem. Some other method as setTarget, setMenuVisible may be also helpful to you.

###GestureRecognizer of UITextView
There are many default gestures with UITextView, double tapping, long press ect. And I find it’s hard to remove these system gestures. When I want to use doubleTap gesture to zoom, the default double tap is always triggered which makes my own gesture failed. The method to disable these default gesture is using UIGestureRecognizerDelegate.

There are three methods in UIGestureRecognizerDelegate protocol.
gestureRecognizer:shouldReceiveTouch:
gestureRecognizerShouldBegin:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
You can override the first two method, and return NO for unwanted default gesture. For more infomation you can refer to UIGestureRecognizerDelegate. But in my experiment, the delegate method is only be called by ios6, in iOS5 the first two delegate methods haven’t been called, I will make more investigation.

###UITextInput protocol
Usually we want to set some text to be selected(highlight), or get user’s selection. UITextInput protocol is responsible for this. We can get user selection, the rect of user selection, and can get caret position. This is a important protocol, wish you can read the reference carefully UITextInpue

A short summary, hope helpful for you.