iOS自訂發送訊息輸入框,ios發送訊息輸入框

來源:互聯網
上載者:User

iOS自訂發送訊息輸入框,ios發送訊息輸入框

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

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


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.