在iOS开发中,UITextView是一个使用还算比较多的控件。但是用过的人都知道,UITextView有很多存在的问题,今天就来一一说它一说。
一、设置textView的placeHolder
首先需要解决的就是占位文字placeHolder的问题,与UITextField相比,UITextView并没有相应的placeholder属性设置占位文字,但是可以通过category的方式给textView添加placeHolder属性,这样就可以在任何需要的使用直接使用了,我写好了一个category UITextView+PlaceHolder,大概如下:
.h中
#import <UIKit/UIKit.h>
@interface UITextView (PlaceHolder)
@property (nonatomic, copy) NSString *placeHolder;
@end
.m中:
#import "UITextView+PlaceHolder.h"
#import <objc/runtime.h>
#define kScreenW [UIScreen mainScreen].bounds.size.width
static const void *textView_key = @"placeHolder";
@interface UITextView ()
@end
@implementation UITextView (PlaceHolder)
- (void)setPlaceHolder:(NSString *)placeHolder
{
if (placeHolder != self.placeHolder) {
objc_setAssociatedObject(self, textView_key, placeHolder, OBJC_ASSOCIATION_COPY_NONATOMIC);
UILabel *placeHolderLb = [[UILabel alloc] initWithFrame:CGRectMake(2, 7, kScreenW-2*16, 21)];
placeHolderLb.tag = 1000;
placeHolderLb.contentMode = UIViewContentModeTop;
placeHolderLb.numberOfLines = 0;
placeHolderLb.textColor = [UIColor redColor];
placeHolderLb.font = [UIFont systemFontOfSize:16];
placeHolderLb.alph