TextView封装

功能:自定义占位文本(设置占位文本的多种属性),可以设置输入的最多字数,并将输入内容用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;
    };

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • 目光猶如人群 大多時候—— 我們不會眺望高樓 更不會注意地基 只注視平凡如己者
    憨憨爹阅读 151评论 0 0
  • wars
    5d3c51893aa6阅读 160评论 0 0
  • 懒得起床,懒得看书,懒得吃饭,懒得走路,懒得动一动,懒得去试试改变自己,又或者是懒得去给父母打一个电话,永远只在...
    漾零阅读 282评论 0 4