OC之UITextView

SONG / 文 发表于2018-01-26 14:54 次阅读 uitextview

UItextView控件与UITextField大致相同; 记录一下不同之处: 一、初始化(相同的都在此处)

  //初始化UITextView
    UITextView *textview =  [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
     //设置背景色
    textview.backgroundColor=[UIColor whiteColor];
      //设置是否允许编辑内容,默认为“YES”
    textview.editable = YES;
    //设置字体名字和字体大小;
    textview.font=[UIFont fontWithName:@"Arial" size:18.0];
    //设置return键的类型
    textview.returnKeyType = UIReturnKeyDefault;
    //设置键盘类型一般为默认
    textview.keyboardType = UIKeyboardTypeDefault;
    //文本显示的位置默认为居左
    textview.textAlignment = NSTextAlignmentLeft;
    //显示数据类型的连接模式(如电话号码、网址、地址等)
    textview.dataDetectorTypes = UIDataDetectorTypeAll;
    // 设置显示文字颜色
    textview.textColor = [UIColor blackColor];
    //设置显示的文本内容
    textview.text = @"UITextView";

二、是否允许滑动

     //设置当文字超过视图的边框时是否允许滑动,默认为“YES”
    textview.scrollEnabled = NO;

三、添加边框

//UITextView没有border属性,所以智能用layer来加边框
textview.layer.backgroundColor = [[UIColor clearColor]CGColor];
 textview.layer.borderColor = [[UIColor redColor]CGColor];
 textview.layer.borderWidth = 1.0;
 [textview.layer setMasksToBounds:YES];

四、相关代理

/**
 将要开始编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}
/**
  开始编辑
 @param textView textView
 */
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"您的意见是我们前进的最大动力,谢谢!"]) {
        textView.text = @"";
    }
}

/**
 将要结束编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}

/**
 结束编辑

 @param textView textView
 */
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1) {
         textView.text = @"您的意见是我们前进的最大动力,谢谢!";
    }
}

五、UITextView的placeholder(本身没有这个属性) 1..h文件

#import <UIKit/UIKit.h>

@interface QSTextView : UITextView
@property(nonatomic) UILabel *placeHolderLabel;
@property(nonatomic) NSString *placeholder;
@property(nonatomic) UIColor *placeholderColor;
@end

2..m文件

#import "QSTextView.h"

@implementation QSTextView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUpSubViews];
    }
    return self;
}

- (void)setUpSubViews
{
    self.text  = @"";
    [self setPlaceholder:@""];
    [self setPlaceholderColor:[UIColor lightGrayColor]];
}

- (void)setText:(NSString *)text {
    [super setText:text];
}
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if( [[self placeholder] length] > 0 )
    {
        if ( _placeHolderLabel == nil )
        {
            _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
            _placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
            _placeHolderLabel.numberOfLines = 0;
            _placeHolderLabel.font = self.font;
            _placeHolderLabel.backgroundColor = [UIColor clearColor];
            _placeHolderLabel.textColor = self.placeholderColor;
            _placeHolderLabel.alpha = 0;
            _placeHolderLabel.tag = 999;
            [self addSubview:_placeHolderLabel];
        }
        _placeHolderLabel.text = self.placeholder;
        [_placeHolderLabel sizeToFit];
        [self sendSubviewToBack:_placeHolderLabel];
    }
    if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
    {
        [[self viewWithTag:999] setAlpha:1];
    }
}

@end

3.控制器

#import "ViewController.h"
#import "QSTextView.h"
@interface ViewController ()<UITextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    QSTextView * textView =[[QSTextView alloc] initWithFrame:CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width - 20, 180)];
    textView.backgroundColor = [UIColor whiteColor];
    textView.placeholderColor = [UIColor lightGrayColor];
    textView.placeholder = @"我确定我实现了";
    textView.font = [UIFont systemFontOfSize:17];
    textView.delegate = self;
    textView.layer.borderWidth =1;
    textView.layer.borderColor =[UIColor redColor].CGColor;
    [textView.layer setCornerRadius:10.0f];
    [self.view addSubview:textView];

}

- (void)textViewDidChange:(QSTextView *)textView

{

    if([textView.placeholder length] == 0)
    {
        return;
    }
    if([textView.placeholder length]  == 0)
    {
        [textView.placeHolderLabel setAlpha:1];
    }
    else
    {
        [textView.placeHolderLabel  setAlpha:0];
    }
    if ([textView.text isEqualToString:@""]) {
        [textView.placeHolderLabel setAlpha:1];
    }
}
@end

最后这个自定义QSTextView,来自MrBrave丶彬彬:https://www.jianshu.com/p/14385b1db4f3

收藏 赞 (0) 踩 (0)