Custom message input box for iOS and message input box for ios

Source: Internet
Author: User

Custom message input box for iOS and message input box for ios

A simple encapsulation is provided, so that you can use it without any trouble during initialization. If you have other requirements, just set the parameters in it.

WJEasyInputTextView. h

//
// WJEasyInputTextView. h
// Input box on the keyboard
//
// Created by apple on 15/6/23.
// Copyright (c) 2015 tqh. All rights reserved.
//

# Import <UIKit/UIKit. h>

/**
* You can use direct initialization or modify attributes.
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; // background color
@ Property (nonatomic, assign) BOOL showLimitNum; // display words
@ Property (nonatomic, assign) NSInteger limitNum; // limit the number of words
@ Property (nonatomic, strong) UIFont * font; // text size

@ End

WJEasyInputTextView. m

//
// WJEasyInputTextView. m
// Input box on the keyboard
//
// Created by apple on 15/6/23.
// Copyright (c) 2015 tqh. All rights reserved.
//

# Import "WJEasyInputTextView. h"

@ Interface WJEasyInputTextView () <UITextViewDelegate> {
UIView * _ bottomView; // comment box
UITextView * _ textView; // input box
UILabel * _ textApl; // Number of words
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-initialize the keyboard listener

-(Void) initNotification {
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: UIKeyboardWillShowNotification object: nil];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];
}

# Pragma mark-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.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: @ "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) + 10, CGRectGetMinY (_ textView. frame), 80, CGRectGetHeight (_ textView. frame ));
[PlBtn addTarget: self action: @ selector (pinglun) forControlEvents: UIControlEventTouchUpInside];
[_ BottomView addSubview: plBtn];
}

# Pragma mark-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;
}

# Pragma mark-event listening

-(Void) pinglun
{
NSLog (@ "send ");
}

-(Void) textViewDidChange :( UITextView *) textView {
If (_ showLimitNum ){
NSString * toBeString = textView. text;
NSArray * currentar = [UITextInputMode activeInputModes];
UITextInputMode * current = [currentar firstObject];

// The following method is discarded by iOS7.
// NSString * lang = [[UITextInputMode currentInputMode] primaryLanguage]; // keyboard input mode

If ([current. primaryLanguage isEqualToString: @ "zh-Hans"]) {// Simplified Chinese input, including simplified Chinese pinyin, five athletes, and simplified handwriting
UITextRange * selectedRange = [textView markedTextRange];
// Obtain the highlighted part
UITextPosition * position = [textView positionFromPosition: selectedRange. start offset: 0];
// If no selected words are highlighted, the words that have been entered are counted and restricted.
If (! Position ){
If (toBeString. length> _ limitNum ){
TextView. text = [toBeString substringToIndex: _ limitNum];
}
}
// If a highlighted string exists, no text statistics and restrictions are imposed.
Else {

}
}
// Statistical restrictions can be directly imposed on Chinese input methods, regardless of other languages
Else {
If (toBeString. length> _ limitNum ){
TextView. text = [toBeString substringToIndex: _ limitNum];
}
}
NSLog (@ "% @", textView. text );
} Else {

}
}


# Pragma mark-keyboard listener

-(Void) keyboardWillShow :( NSNotification *) notification
{
// Obtain the keyboard height
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

:



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.