免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1482 | 回复: 0
打印 上一主题 下一主题

[iOS] iOS自定义发送消息输入框 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-23 14:10 |只看该作者 |倒序浏览
简单的封装了一个,免得麻烦直接初始化就可以用了 ,有其他需求该里面参数就行了

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

  8. #import <UIKit/UIKit.h>

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

  18. @interface WJEasyInputTextView : UIView

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

  23. @end
复制代码
WJEasyInputTextView.m
  1. //
  2. //  WJEasyInputTextView.m
  3. //  键盘上的输入框
  4. //
  5. //  Created by apple on 15/6/23.
  6. //  Copyright (c) 2015年 tqh. All rights reserved.
  7. //

  8. #import "WJEasyInputTextView.h"

  9. @interface WJEasyInputTextView ()<UITextViewDelegate> {
  10.     UIView *_bottomView;//评论框
  11.     UITextView *_textView;//输入框
  12.     UILabel *_textApl;//字数
  13.     CGRect _rect;
  14. }
  15. @end

  16. @implementation WJEasyInputTextView

  17. - (instancetype)init
  18. {
  19.     self = [super init];
  20.     if (self) {
  21.         self.frame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-50, CGRectGetWidth([UIScreen mainScreen].bounds), 50);
  22.         _rect = self.frame;
  23.         [self initNotification];
  24.         [self AddtextFieldComments];
  25.     }
  26.     return self;
  27. }


  28. #pragma mark - 初始化键盘监听

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

  33. #pragma mark - 初始化视图

  34. - (void)AddtextFieldComments  {
  35.     _bottomView = [[UIView alloc] initWithFrame:self.bounds];
  36.     _bottomView.backgroundColor = self.bgColor;
  37.     _bottomView.userInteractionEnabled= YES;
  38.     [self addSubview:_bottomView];
  39.    
  40.     UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 0.5)];
  41.     lineView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.3];
  42.     [_bottomView addSubview:lineView];
  43.    
  44.     _textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 5, CGRectGetWidth(self.bounds) - 115, 40)];
  45.     _textView.layer.cornerRadius = 6;
  46.     _textView.layer.borderWidth = 1;
  47.     _textView.delegate = self;
  48.     _textView.font = [UIFont systemFontOfSize:13];
  49.     _textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  50.     _textView.autocorrectionType = UITextAutocorrectionTypeNo;
  51.     _textView.layer.borderColor = lineView.backgroundColor.CGColor;
  52.     [_bottomView addSubview:_textView];
  53.    
  54.     _textApl = [[UILabel alloc] init];
  55.     _textApl.frame = CGRectMake(CGRectGetMaxX(_textView.frame)-37, 35, 30, 6);
  56.     _textApl.textColor = [UIColor grayColor];
  57.     _textApl.textAlignment = NSTextAlignmentRight;
  58.     _textApl.font = [UIFont systemFontOfSize:8];
  59. //    _textApl.text = @"140";
  60.     [_bottomView addSubview:_textApl];
  61.    
  62.     UIButton *plBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  63.     plBtn.layer.borderWidth = 1;
  64.     plBtn.backgroundColor = [UIColor whiteColor];
  65.     [plBtn setTitle:@"发送" forState:UIControlStateNormal];
  66.     [plBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  67.     plBtn.layer.cornerRadius = 6;
  68.     plBtn.layer.borderColor = lineView.backgroundColor.CGColor;
  69.     plBtn.frame = CGRectMake(CGRectGetMaxX(_textView.frame) + 10, CGRectGetMinY(_textView.frame), 80, CGRectGetHeight(_textView.frame));
  70.     [plBtn addTarget:self action:@selector(pinglun) forControlEvents:UIControlEventTouchUpInside];
  71.     [_bottomView addSubview:plBtn];
  72. }

  73. #pragma mark - get方法

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

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

  83. - (void)setShowLimitNum:(BOOL)showLimitNum {
  84.     _showLimitNum = showLimitNum;
  85.     if (showLimitNum) {
  86.         _textApl.hidden = NO;
  87.     }else {
  88.         _textApl.hidden = YES;
  89.     }
  90. }

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

  95. #pragma mark - 事件监听

  96. - (void)pinglun
  97. {
  98.     NSLog(@"发送");
  99. }

  100. - (void)textViewDidChange:(UITextView *)textView {
  101.     if (_showLimitNum) {
  102.         NSString *toBeString = textView.text;
  103.         NSArray *currentar = [UITextInputMode activeInputModes];
  104.         UITextInputMode *current = [currentar firstObject];
  105.         
  106.         //下面的方法是iOS7被废弃的,注释
  107.         //    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
  108.         
  109.         if ([current.primaryLanguage isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
  110.             UITextRange *selectedRange = [textView markedTextRange];
  111.             //获取高亮部分
  112.             UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
  113.             // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
  114.             if (!position) {
  115.                 if (toBeString.length > _limitNum) {
  116.                     textView.text = [toBeString substringToIndex:_limitNum];
  117.                 }
  118.             }
  119.             // 有高亮选择的字符串,则暂不对文字进行统计和限制
  120.             else{
  121.                
  122.             }
  123.         }
  124.         // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
  125.         else{
  126.             if (toBeString.length > _limitNum) {
  127.                 textView.text = [toBeString substringToIndex:_limitNum];
  128.             }
  129.         }
  130.         NSLog(@"%@",textView.text);
  131.     }else {
  132.         
  133.     }
  134. }


  135. #pragma mark - 键盘监听

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

  146. - (void)keyboardWillHide:(NSNotification *)notification
  147. {
  148.     //-49
  149.     self.frame = _rect;
  150. }

  151. @end
复制代码
效果图:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP