iOS custom Send message input box

Source: Internet
Author: User

The simple encapsulation of a, so that the trouble of direct initialization can be used, there are other requirements of the parameters on the line

WJEasyInputTextView.h

//
//WJEasyInputTextView.h
//input boxes on the keyboard
//
//Created by Apple on 15/6/23.
//Copyright (c) 2015 TQH. All rights reserved.
//

#import<UIKit/UIKit.h>

/**
* Using direct initialization, you can also change properties
Wjeasyinputtextview *WJ = [[Wjeasyinputtextview alloc]init];
Wj.bgcolor = [Uicolor Orangecolor];
Wj.showlimitnum = YES;
Wj.font = [Uifont systemfontofsize:18];
Wj.limitnum = 13;
[Self.view ADDSUBVIEW:WJ];
*/

@interfaceWjeasyinputtextview:uiview

@property (Nonatomic,strong) Uicolor *bgcolor;//Background Color
@property (nonatomic,assign) BOOL showlimitnum;//Display Word Count
@property (nonatomic,assign) Nsinteger limitnum;//limit The number of words
@property (Nonatomic,strong) Uifont *font;//Text Size

@end

wjeasyinputtextview.m

//
//wjeasyinputtextview.m
//input boxes on the keyboard
//
//Created by Apple on 15/6/23.
//Copyright (c) 2015 TQH. All rights reserved.
//

#import"WJEasyInputTextView.h"

@interfaceWjeasyinputtextview () <UITextViewDelegate> {
UIView *_bottomview;//Comment Box
Uitextview *_textview;//Input Box
UILabel *_TEXTAPL;//Word Count
CGRect _rect;
}
@end

@implementationWjeasyinputtextview

-(Instancetype) init
{
self = [super init];
if(self) {
Self.frame = CGRectMake (0, Cgrectgetheight ([UIScreen mainscreen].bounds)- -, Cgrectgetwidth ([UIScreen mainscreen].bounds), -);
_rect = Self.frame;
[Self initnotification];
[Self addtextfieldcomments];
}
returnSelf
}


#pragmaMark-Initialize keyboard monitoring

- (void) Initnotification {
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) Name: UikeyboardwillshownotificationObject: nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillhide:) Name: UikeyboardwillhidenotificationObject: nil];
}

#pragmaMark-Initialize View

- (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.6Alpha0.3];
[_bottomview Addsubview:lineview];

_textview = [[Uitextview alloc] Initwithframe:cgrectmake ( the,5, Cgrectgetwidth (Self.bounds)- the, +)];
_textview.layer.cornerradius =6;
_textview.layer.borderwidth =1;
_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)-Panax Notoginseng, *, -,6);
_textapl.textcolor = [Uicolor Graycolor];
_textapl.textalignment = Nstextalignmentright;
_textapl.font = [Uifont systemfontofsize:8];
//_textapl.text = @ " +";
[_bottomview ADDSUBVIEW:_TEXTAPL];

UIButton *plbtn = [UIButton buttonwithtype:uibuttontyperoundedrect];
PlBtn.layer.borderWidth =1;
Plbtn.backgroundcolor = [Uicolor Whitecolor];
[Plbtn Settitle:@"Send"Forstate:uicontrolstatenormal];
[Plbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
PlBtn.layer.cornerRadius =6;
PlBtn.layer.borderColor = LineView.backgroundColor.CGColor;
Plbtn.frame = CGRectMake (Cgrectgetmaxx (_textview.frame) +Ten, Cgrectgetminy (_textview.frame), the, Cgrectgetheight (_textview.frame));
[Plbtn addtarget:self Action: @selector (Pinglun) forcontrolevents:uicontroleventtouchupinside];
[_bottomview ADDSUBVIEW:PLBTN];
}

#pragmaMark-get method

- (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;
}

#pragmaMark-Event Monitoring

- (void) Pinglun
{
NSLog (@"Send");
}

- (void) Textviewdidchange: (Uitextview *) TextView {
if(_showlimitnum) {
NSString *tobestring = Textview.text;
Nsarray *currentar = [Uitextinputmode activeinputmodes];
Uitextinputmode *current = [Currentar firstobject];

//The following methods are iOS7 discarded, annotated
//nsstring *lang = [[Uitextinputmode currentinputmode] primarylanguage];//Keyboard Input Mode

if([Current.primarylanguage isequaltostring:@"Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting
Uitextrange *selectedrange = [TextView markedtextrange];
//get highlighted parts
Uitextposition *position = [TextView positionFromPosition:selectedRange.start offset:0];
//Word Count and limit the words you have entered without highlighting the selected words
if(!position) {
if(Tobestring.length > _limitnum) {
Textview.text = [tobestring substringtoindex:_limitnum];
}
}
//there is a highlighted string, the text is temporarily not counted and limited
Else{

}
}
//Direct statistical restrictions other than Chinese input method, regardless of other languages
Else{
if(Tobestring.length > _limitnum) {
Textview.text = [tobestring substringtoindex:_limitnum];
}
}
NSLog (@"%@", Textview.text);
}Else{

}
}


#pragmaMark-Keyboard Monitor

- (void) Keyboardwillshow: (nsnotification *) notification
{
//Get the keyboard height
Nsdictionary *userinfo = [notification UserInfo];
nsvalue* avalue = [UserInfo Objectforkey:uikeyboardframeenduserinfokey];
CGRect keyboardrect = [Avalue cgrectvalue];
//--
Self.frame = CGRectMake (0, Cgrectgetheight ([UIScreen mainscreen].bounds)-KeyboardRect.size.height- -, Cgrectgetwidth (_bottomview.frame), Cgrectgetheight (_bottomview.frame));

}

- (void) Keyboardwillhide: (nsnotification *) notification
{
//-49
Self.frame = _rect;
}

@end

:



iOS custom Send message input box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.