你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> iOS自定義發送消息輸入框

iOS自定義發送消息輸入框

編輯:IOS技巧綜合
[摘要]本文是對iOS自定義發送消息輸入框的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。

簡單的封裝了一個,免得麻煩直接初始化就可以用了 ,有其他需求該裡面參數就行了

WJEasyInputTextView.h

//
//WJEasyInputTextView.h
//鍵盤上的輸入框
//
//Createdbyappleon15/6/23.
//Copyright(c)2015年tqh.Allrightsreserved.
//

#import<UIKit/UIKit.h>

/**
*使用直接初始化,也可以改屬性
WJEasyInputTextView*wj=[[WJEasyInputTextViewalloc]init];
wj.bgColor=[UIColororangeColor];
wj.showLimitNum=YES;
wj.font=[UIFontsystemFontOfSize:18];
wj.limitNum=13;
[self.viewaddSubview:wj];
*/

@interfaceWJEasyInputTextView:UIView

@property(nonatomic,strong)UIColor*bgColor;//背景色
@property(nonatomic,assign)BOOLshowLimitNum;//顯示字數
@property(nonatomic,assign)NSIntegerlimitNum;//限制字數
@property(nonatomic,strong)UIFont*font;//文字大小

@end

WJEasyInputTextView.m

//
//WJEasyInputTextView.m
//鍵盤上的輸入框
//
//Createdbyappleon15/6/23.
//Copyright(c)2015年tqh.Allrightsreserved.
//

#import"WJEasyInputTextView.h"

@interfaceWJEasyInputTextView()<UITextViewDelegate>{
UIView*_bottomView;//評論框
UITextView*_textView;//輸入框
UILabel*_textApl;//字數
CGRect_rect;
}
@end

@implementationWJEasyInputTextView

-(instancetype)init
{
self=[superinit];
if(self){
self.frame=CGRectMake(0,CGRectGetHeight([UIScreenmainScreen].bounds)-50,CGRectGetWidth([UIScreenmainScreen].bounds),50);
_rect=self.frame;
[selfinitNotification];
[selfAddtextFieldComments];
}
returnself;
}


#pragmamark-初始化鍵盤監聽

-(void)initNotification{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}

#pragmamark-初始化視圖

-(void)AddtextFieldComments{
_bottomView=[[UIViewalloc]initWithFrame:self.bounds];
_bottomView.backgroundColor=self.bgColor;
_bottomView.userInteractionEnabled=YES;
[selfaddSubview:_bottomView];

UIView*lineView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.bounds),0.5)];
lineView.backgroundColor=[UIColorcolorWithWhite:0.6alpha:0.3];
[_bottomViewaddSubview:lineView];

_textView=[[UITextViewalloc]initWithFrame:CGRectMake(15,5,CGRectGetWidth(self.bounds)-115,40)];
_textView.layer.cornerRadius=6;
_textView.layer.borderWidth=1;
_textView.delegate=self;
_textView.font=[UIFontsystemFontOfSize:13];
_textView.autocapitalizationType=UITextAutocapitalizationTypeNone;
_textView.autocorrectionType=UITextAutocorrectionTypeNo;
_textView.layer.borderColor=lineView.backgroundColor.CGColor;
[_bottomViewaddSubview:_textView];

_textApl=[[UILabelalloc]init];
_textApl.frame=CGRectMake(CGRectGetMaxX(_textView.frame)-37,35,30,6);
_textApl.textColor=[UIColorgrayColor];
_textApl.textAlignment=NSTextAlignmentRight;
_textApl.font=[UIFontsystemFontOfSize:8];
//_textApl.text=@"140";
[_bottomViewaddSubview:_textApl];

UIButton*plBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
plBtn.layer.borderWidth=1;
plBtn.backgroundColor=[UIColorwhiteColor];
[plBtnsetTitle:@"發送"forState:UIControlStateNormal];
[plBtnsetTitleColor:[UIColorblackColor]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));
[plBtnaddTarget:selfaction:@selector(pinglun)forControlEvents:UIControlEventTouchUpInside];
[_bottomViewaddSubview:plBtn];
}

#pragmamark-get方法

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

-(void)setLimitNum:(NSInteger)limitNum{
NSLog(@"%ld",limitNum);
_limitNum=limitNum;
_textApl.text=[NSStringstringWithFormat:@"%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;
}

#pragmamark-事件監聽

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

-(void)textViewDidChange:(UITextView*)textView{
if(_showLimitNum){
NSString*toBeString=textView.text;
NSArray*currentar=[UITextInputModeactiveInputModes];
UITextInputMode*current=[currentarfirstObject];

//下面的方法是iOS7被廢棄的,注釋
//NSString*lang=[[UITextInputModecurrentInputMode]primaryLanguage];//鍵盤輸入模式

if([current.primaryLanguageisEqualToString:@"zh-Hans"]){//簡體中文輸入,包括簡體拼音,健體五筆,簡體手寫
UITextRange*selectedRange=[textViewmarkedTextRange];
//獲取高亮部分
UITextPosition*position=[textViewpositionFromPosition:selectedRange.startoffset:0];
//沒有高亮選擇的字,則對已輸入的文字進行字數統計和限制
if(!position){
if(toBeString.length>_limitNum){
textView.text=[toBeStringsubstringToIndex:_limitNum];
}
}
//有高亮選擇的字符串,則暫不對文字進行統計和限制
else{

}
}
//中文輸入法以外的直接對其統計限制即可,不考慮其他語種情況
else{
if(toBeString.length>_limitNum){
textView.text=[toBeStringsubstringToIndex:_limitNum];
}
}
NSLog(@"%@",textView.text);
}else{

}
}


#pragmamark-鍵盤監聽

-(void)keyboardWillShow:(NSNotification*)notification
{
//得到鍵盤高度
NSDictionary*userInfo=[notificationuserInfo];
NSValue*aValue=[userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];
CGRectkeyboardRect=[aValueCGRectValue];
//-49
self.frame=CGRectMake(0,CGRectGetHeight([UIScreenmainScreen].bounds)-keyboardRect.size.height-50,CGRectGetWidth(_bottomView.frame),CGRectGetHeight(_bottomView.frame));

}

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

@end
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved