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

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(, CGRectGetHeight([UIScreen mainScreen].bounds)-, CGRectGetWidth([UIScreen mainScreen].bounds), );
        _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(, , CGRectGetWidth(self.bounds), 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.3];
    [_bottomView addSubview:lineView];
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(, , CGRectGetWidth(self.bounds) - , )];
    _textView.layer.cornerRadius = ;
    _textView.layer.borderWidth = ;
    _textView.delegate = self;
    _textView.font = [UIFont systemFontOfSize:];
    _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)-, , , );
    _textApl.textColor = [UIColor grayColor];
    _textApl.textAlignment = NSTextAlignmentRight;
    _textApl.font = [UIFont systemFontOfSize:];
//    _textApl.text = @"140";
    [_bottomView addSubview:_textApl];
    
    UIButton *plBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    plBtn.layer.borderWidth = ;
    plBtn.backgroundColor = [UIColor whiteColor];
    [plBtn setTitle:@"发送" forState:UIControlStateNormal];
    [plBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    plBtn.layer.cornerRadius = ;
    plBtn.layer.borderColor = lineView.backgroundColor.CGColor;
    plBtn.frame = CGRectMake(CGRectGetMaxX(_textView.frame) + , CGRectGetMinY(_textView.frame), , 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:];
            // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
            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(, CGRectGetHeight([UIScreen mainScreen].bounds) - keyboardRect.size.height - , CGRectGetWidth(_bottomView.frame), CGRectGetHeight(_bottomView.frame));
    
} - (void)keyboardWillHide:(NSNotification *)notification
{
    //-49
    self.frame = _rect;
} @end

效果图:

iOS自定义发送消息输入框的更多相关文章

  1. openfire自定义发送消息

    加入以下类: 这个是xml格式的,解析时可以将xml转成map,参数可自由定义 import org.jivesoftware.smack.packet.PacketExtension; /** * ...

  2. iOS端给unity发送消息,实现两者交互。

    上一篇我们简单说了一下unity发消息给iOS端.现在我们就来说一下iOS端给unity发送消息的简单使用. 首先iOS端做得事情其实很简单就一句话,直接上代码 /** * 第一个参数:是unity那 ...

  3. 钉钉机器人集成Jenkins推送消息模板自定义发送报告

    一.由于公司同样也使用了钉钉.那么在做Jenkins集成自动化部署的时候,也是可以集成钉钉的. 那种Jenkins下载钉钉插件集成,简单设置就可以完成了.我们今天要做的是,定制化的发送消息. 钉钉推送 ...

  4. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  5. 【读书笔记】iOS-ARC-不要向已经释放的对象发送消息

    一,在AppDelegate.m中写入如下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...

  6. IOS 推送消息 php做推送服务端

    IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPh ...

  7. iOS NSNotificationCenter(消息机制)

    转自:http://blog.csdn.net/liliangchw/article/details/8276803 对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notificatio ...

  8. 自定义WM_NOTIFY消息

    自定义WM_NOTIFY消息 习惯了用自定义用户消息进行各种状态的通知,特别是子窗口与父窗口之间的交互.但ON_MESSAGE没有控件ID的限制,如果有多个子窗口发送同一个消息给父窗口时,父窗口就不知 ...

  9. IOS 基于APNS消息推送原理与实现(JAVA后台)

    Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Pu ...

随机推荐

  1. 算法系列8《Base64》

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单 ...

  2. Android 实现子View的状态跟随父容器的状态

    最近自学着做东西,需要做一个效果,就是我ListView的Item点击下或者选中的时候,我Item里面的各个组件的状态要和我Item的状态保持一直,这样我就可以用XML,去根据组件的不同状态去实现不同 ...

  3. VS2010遇到_WIN32_WINNT宏定义问题

    最近拿到一个别人的工程,是使用VS.net创建的,而我的机器上只有vs2010,于是用自带的转换工具将它转换成vs2010的工程,转换之前我就很担心,怕转换完后会出问题,但是没有办法,我实在是不想再安 ...

  4. vnextcn

    Flag 标题 通过 提交 AC% 难度   E1 编写Hello World网站 9 17 52% 1   E2 编写Hello World控制台程序 11 13 84% 1   E3 控制器基础练 ...

  5. CoreLocation简单应用

    1.获取locationManager let locationManager: CLLocationManager = CLLocationManager() 2.设置locationManager ...

  6. [转]p2p端口映射工具 dog-tunnel

    [转]p2p端口映射工具 dog-tunnel http://www.oschina.net/p/dog-tunnel 狗洞是一个高速的 P2P 端口映射工具,同时支持Socks5代理. 0.5版后开 ...

  7. ALTERA MAX10官方评估板,新鲜出炉!

    刚刚拿到骏龙提供的ALTERA MAX10官方评估板,还热乎呢,呵呵!赶紧跟大家分享一下 板子很简单,把IO口都扩展出来了,其他功能基本上没有. FPGA型号是10M08SAE144C8GES,144 ...

  8. verilog实现16位五级流水线的CPU带Hazard冲突处理

    verilog实现16位五级流水线的CPU带Hazard冲突处理 该文是基于博主之前一篇博客http://www.cnblogs.com/wsine/p/4292869.html所增加的Hazard处 ...

  9. homework-01 "最大子数组之和"的解决过程

    看到这个题目,我首先想到就是暴力解决 求出所有的子数组的和,取出最大值即可 但其中是可以有优化的 如 子数组[3:6]可以用[3:5]+[6]来计算 即可以将前面的计算结果保留下来,减少后面的重复计算 ...

  10. 如何给ActiveX控件添加“事件”“属性”“标准事件”“自定义事件”等一些相关操作

    上一篇小编带大家熟悉了一下ActiveX的建立以及相关的概念,(http://blog.csdn.net/u014028070/article/details/38424611) 本文介绍下如何给控件 ...