iOS自定义发送消息输入框

简单的封装了一个,免得麻烦直接初始化就可以用了 ,有其他需求该里面参数就行了

WJEasyInputTextView.h

//
//   WJEasyInputTextView.h
//   键盘上的输入框
//
//   Created by apple on 15/6/23.
//   Copyright (c) 2015年 tqh. All rights reserved.
//

#import <UIKit/UIKit.h>

/* *
 *  使用 直接初始化,也可以改属性
 WJEasyInputTextView *wj = [[WJEasyInputTextView alloc]init];
 wj.bgColor = [UIColor orangeColor];
 wj.showLimitNum = YES;
 wj.font = [UIFont systemFontOfSize:18];
 wj.limitNum = 13;
 [self.view addSubview:wj];
 
*/

@interface WJEasyInputTextView : UIView

@property (nonatomic,strong)UIColor *bgColor;    // 背景色
@property (nonatomic,assign)BOOL showLimitNum;  // 显示字数
@property (nonatomic,assign)NSInteger limitNum;  // 限制字数
@property (nonatomic,strong)UIFont *font;        // 文字大小

@end

 WJEasyInputTextView.m

//
//   WJEasyInputTextView.m
//   键盘上的输入框
//
//   Created by apple on 15/6/23.
//   Copyright (c) 2015年 tqh. All rights reserved.
//

#import  " WJEasyInputTextView.h "

@interface WJEasyInputTextView ()<UITextViewDelegate> {
    UIView *_bottomView; // 评论框
    UITextView *_textView; // 输入框
    UILabel *_textApl; // 字数
    CGRect _rect;
}
@end

@implementation WJEasyInputTextView

- (instancetype)init
{
    self = [super init];
     if (self) {
        self.frame = CGRectMake( 0, CGRectGetHeight([UIScreen mainScreen].bounds)- 50, CGRectGetWidth([UIScreen mainScreen].bounds),  50);
        _rect = self.frame;
        [self initNotification];
        [self AddtextFieldComments];
    }
     return self;
}


#pragma mark - 初始化键盘监听

- ( void)initNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification  object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification  object:nil];
}

#pragma mark - 初始化视图

- ( void)AddtextFieldComments  {
    _bottomView = [[UIView alloc] initWithFrame:self.bounds];
    _bottomView.backgroundColor = self.bgColor;
    _bottomView.userInteractionEnabled= YES;
    [self addSubview:_bottomView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake( 00, CGRectGetWidth(self.bounds),  0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite: 0.6 alpha: 0.3];
    [_bottomView addSubview:lineView];
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake( 155, CGRectGetWidth(self.bounds) -  11540)];
    _textView.layer.cornerRadius =  6;
    _textView.layer.borderWidth =  1;
    _textView. delegate = self;
    _textView.font = [UIFont systemFontOfSize: 13];
    _textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _textView.autocorrectionType = UITextAutocorrectionTypeNo;
    _textView.layer.borderColor = lineView.backgroundColor.CGColor;
    [_bottomView addSubview:_textView];
    
    _textApl = [[UILabel alloc] init];
    _textApl.frame = CGRectMake(CGRectGetMaxX(_textView.frame)- 3735306);
    _textApl.textColor = [UIColor grayColor];
    _textApl.textAlignment = NSTextAlignmentRight;
    _textApl.font = [UIFont systemFontOfSize: 8];
//     _textApl.text = @"140";
    [_bottomView addSubview:_textApl];
    
    UIButton *plBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    plBtn.layer.borderWidth =  1;
    plBtn.backgroundColor = [UIColor whiteColor];
    [plBtn setTitle: @" 发送 " forState:UIControlStateNormal];
    [plBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    plBtn.layer.cornerRadius =  6;
    plBtn.layer.borderColor = lineView.backgroundColor.CGColor;
    plBtn.frame = CGRectMake(CGRectGetMaxX(_textView.frame) +  10, CGRectGetMinY(_textView.frame),  80, CGRectGetHeight(_textView.frame));
    [plBtn addTarget:self action:@selector(pinglun) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:plBtn];
}

#pragma mark - get方法

- ( void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    _bottomView.backgroundColor = bgColor;
}

- ( void)setLimitNum:(NSInteger)limitNum {
    NSLog( @" %ld ",limitNum);
    _limitNum = limitNum;
    _textApl.text = [NSString stringWithFormat: @" %ld ",limitNum];
}

- ( void)setShowLimitNum:(BOOL)showLimitNum {
    _showLimitNum = showLimitNum;
     if (showLimitNum) {
        _textApl.hidden = NO;
    } else {
        _textApl.hidden = YES;
    }
}

- ( void)setFont:(UIFont *)font {
    _font = font;
    _textView.font = font;
}

#pragma mark - 事件监听

- ( void)pinglun
{
    NSLog( @" 发送 ");
}

- ( void)textViewDidChange:(UITextView *)textView {
     if (_showLimitNum) {
        NSString *toBeString = textView.text;
        NSArray *currentar = [UITextInputMode activeInputModes];
        UITextInputMode *current = [currentar firstObject];
        
         // 下面的方法是iOS7被废弃的,注释
        
//     NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];  //  键盘输入模式
        
         if ([current.primaryLanguage isEqualToString: @" zh-Hans "]) {  //  简体中文输入,包括简体拼音,健体五笔,简体手写
            UITextRange *selectedRange = [textView markedTextRange];
             // 获取高亮部分
            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset: 0];
             //  没有高亮选择的字,则对已输入的文字进行字数统计和限制
             if (!position) {
                 if (toBeString.length > _limitNum) {
                    textView.text = [toBeString substringToIndex:_limitNum];
                }
            }
             //  有高亮选择的字符串,则暂不对文字进行统计和限制
             else{
                
            }
        }
         //  中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
         else{
             if (toBeString.length > _limitNum) {
                textView.text = [toBeString substringToIndex:_limitNum];
            }
        }
        NSLog( @" %@ ",textView.text);
    } else {
        
    }
}


#pragma mark - 键盘监听

- ( void)keyboardWillShow:(NSNotification *)notification
{
     // 得到键盘高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
     //  - 49
    self.frame = CGRectMake( 0, CGRectGetHeight([UIScreen mainScreen].bounds) - keyboardRect.size.height -  50, CGRectGetWidth(_bottomView.frame), CGRectGetHeight(_bottomView.frame));
    
}

- ( void)keyboardWillHide:(NSNotification *)notification
{
     // -49
    self.frame = _rect;
}

@end

效果图:


 

转载于:https://www.cnblogs.com/hxwj/p/4595122.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值