標籤:
簡單的封裝了一個,免得麻煩直接初始化就可以用了 ,有其他需求該裡面參數就行了
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(0, 0, CGRectGetWidth(self.bounds), 0.5)];
lineView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.3];
[_bottomView addSubview:lineView];
_textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 5, CGRectGetWidth(self.bounds) - 115, 40)];
_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)-37, 35, 30, 6);
_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{
}
}
// 中文IME以外的直接對其統計限制即可,不考慮其他語種情況
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
:
iOS自訂發送訊息輸入框