美文网首页
TextView封装

TextView封装

作者: 丨十三叔丨 | 来源:发表于2017-09-18 11:42 被阅读0次

    功能:自定义占位文本(设置占位文本的多种属性),可以设置输入的最多字数,并将输入内容用Block方法返回给VC界面。

    LDNewTextView.h文件

    @interface LDNewTextView : UIView
    
    -(instancetype)initWithFrame:(CGRect)frame withFatherViewController:(UIViewController *)vc placeHoldStr:(NSString *)holdStr;
    
    
    
    @property(nonatomic,strong)UITextView *textView;
    @property(nonatomic,strong)UIColor *textColor;
    @property(nonatomic,assign)NSInteger textFont;
    
    
    @property(nonatomic,strong)UIColor *holdTextColor;
    @property(nonatomic,assign)NSInteger holdTextFont;
    @property(nonatomic,assign)NSInteger maxWordsInt;
    @property(nonatomic,copy)NSString *noticeStr;
    
    @property(nonatomic,copy)void(^returnTextStr)(NSString *str);
    
    @end
    
    

    LDNewTextView.h文件

    #import "LDNewTextView.h"
    #import "factory.h"
    @interface LDNewTextView ()<UITextViewDelegate>
    
    @property(nonatomic,strong)LDLabel *placeHoldLab;
    @property(nonatomic,strong)UIViewController *vc;
    @property(nonatomic,copy)NSString *holdTextStr;
    @end
    
    @implementation LDNewTextView
    
    -(instancetype)initWithFrame:(CGRect)frame withFatherViewController:(UIViewController *)vc placeHoldStr:(NSString *)holdStr{
        self = [super initWithFrame:frame];
        if (self) {
            _vc = vc;
            _holdTextStr = holdStr;
        }
        return self;
    }
    
    
    #pragma mark ------------------协议方法-------------------------------------
    -(void)textViewDidBeginEditing:(UITextView *)textView{
        if (textView == self.textView) {
            self.placeHoldLab.hidden = YES;
            
        }
    }
    
    -(void)textViewDidEndEditing:(UITextView *)textView{
        if (textView == self.textView) {
            if ([self.textView.text isEqualToString:@""]) {
                self.placeHoldLab.hidden = NO;
            }
            
        }
    }
    
    -(void)textViewDidChange:(UITextView *)textView{
        if (textView == self.textView) {
            
            
            if (self.maxWordsInt) {
                if (textView.text.length > self.maxWordsInt) {
                    textView.text = [textView.text substringToIndex:self.maxWordsInt];
                    
                    NSString *noStr;
                    if ([NSString isNotEmptyString:self.noticeStr]) {
                        noStr = self.noticeStr;
                        [self.vc showHudInView:self.vc.view showHint:(noStr)];
                    }else{
                        noStr = [NSString stringWithFormat:@"输入的内容不得超过%zd个字",self.maxWordsInt];
                        [self.vc showHudInView:self.vc.view showHint:(noStr)];
                    }
                    [self.textView resignFirstResponder];
                }
                if (self.returnTextStr) {
                    self.returnTextStr(textView.text);
                }
                
            }else{
                if (self.returnTextStr) {
                    self.returnTextStr(textView.text);
                }
            }
        }
    }
    
    -(void)drawRect:(CGRect)rect{
        
        
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, WIDTH(self), HEIGHT(self))];
        self.textView .delegate = self;
        self.textView.scrollEnabled = YES;
        [self addSubview:self.textView];
        
        if (self.textColor) {
            self.textView.textColor = self.textColor;
        }else{
            self.textView.textColor = UIColorHex(#333333);
        }
        if (self.textFont) {
            self.textView.font = SYSTEMFONT(self.textFont);
        }else{
            self.textView.font = SYSTEMFONT(16);
        }
        
        
        
        UIColor *holdColor;
        float holdFont;
        NSString *holdTextStr;
        
        //字体颜色
        if (self.holdTextColor) {
            holdColor = self.holdTextColor;
        }else{
            holdColor = UIColorHex(#999999);
        }
        
        //字体大小
        if (!self.holdTextFont) {
            holdFont = 16;
        }else{
            holdFont = self.holdTextFont;
        }
        
        //占位的内容
        if ([NSString isNotEmptyString:self.holdTextStr]) {
            holdTextStr = self.holdTextStr;
        }else{
            holdTextStr = @"请输入内容";
        }
        
        _placeHoldLab = [LDLabel labelWithFrame:CGRectMake(12, 5, self.textView.width - 24, 30) withTitle:holdTextStr withFont:holdFont withtextColor:holdColor withBackColor:nil];
        _placeHoldLab.userInteractionEnabled = NO;
        _placeHoldLab.textAlignment = NSTextAlignmentLeft;
    
        [self.textView addSubview:_placeHoldLab];
    }
    @end
    
    

    使用Block反向传值,将内容传入到VC界面中

        newTextView.returnTextStr = ^(NSString *str) {
            DebugLog(@"%@",str);
            self.reasonStr = str;
        };
    

    项目中一些其他文件不便于展示,直接复制会有很多报错信息,等待以后将其他方法独立出来,将代码路径放上,就可以复用。

    相关文章

      网友评论

          本文标题:TextView封装

          本文链接:https://www.haomeiwen.com/subject/jtovsxtx.html