UITextView的高度有輸入文字高度決定

//1.在.h文件中

#import#define MaxTextViewHeight 80 //限制文字輸入的高度

@interface EwenTextView : UIView

//------ 發(fā)送文本 -----//

@property (nonatomic,copy) void (^EwenTextViewBlock)(NSString *text);

//------? 設(shè)置占位符 ------//

- (void)setPlaceholderText:(NSString *)text;

@end

// 2. 在.m文件中

//需要注意的是祭饭,demo依賴于第三方庫Masonry

#import "EwenTextView.h"

#import "Masonry.h"

#define kScreenBounds ([[UIScreen mainScreen] bounds])

#define kScreenwidth (kScreenBounds.size.width)

#define kScreenheight (kScreenBounds.size.height)

#define UIColorRGB(x,y,z) [UIColor colorWithRed:x/255.0 green:y/255.0 blue:z/255.0 alpha:1.0]

@interface EwenTextView(){

BOOL statusTextView;//當(dāng)文字大于限定高度之后的狀態(tài)

NSString *placeholderText;//設(shè)置占位符的文字

}

@property (nonatomic, strong) UIView *backGroundView;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *placeholderLabel;

@property (nonatomic, strong) UIButton *sendButton;

@end

@implementation EwenTextView

- (instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];

if (self) {

[self createUI];

//增加監(jiān)聽报强,當(dāng)鍵盤出現(xiàn)或改變時(shí)收出消息

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];

//增加監(jiān)聽,當(dāng)鍵退出時(shí)收出消息

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification

object:nil];

}

/**

點(diǎn)擊 空白區(qū)域取消

*/

UITapGestureRecognizer *centerTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(centerTapClick)];

[self addGestureRecognizer:centerTap];

return self;

}

- (void)createUI{

[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(6);

make.left.mas_equalTo(5);

make.bottom.mas_equalTo(-6);

make.width.mas_equalTo(kScreenwidth-65);

}];

[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(5);

make.left.mas_equalTo(10);

make.height.mas_equalTo(39);

}];

[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(8);

make.right.mas_equalTo(-5);

make.width.mas_equalTo(50);

}];

}

//暴露的方法

- (void)setPlaceholderText:(NSString *)text{

placeholderText = text;

self.placeholderLabel.text = placeholderText;

}

//當(dāng)鍵盤出現(xiàn)或改變時(shí)調(diào)用

- (void)keyboardWillShow:(NSNotification *)aNotification

{

self.frame = kScreenBounds;

//獲取鍵盤的高度

NSDictionary *userInfo = [aNotification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

int height = keyboardRect.size.height;

if (self.textView.text.length == 0) {

self.backGroundView.frame = CGRectMake(0, kScreenheight-height-49, kScreenwidth, 49);

}else{

CGRect rect = CGRectMake(0, kScreenheight - self.backGroundView.frame.size.height-height, kScreenwidth, self.backGroundView.frame.size.height);

self.backGroundView.frame = rect;

}

}

//當(dāng)鍵退出時(shí)調(diào)用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

if (self.textView.text.length == 0) {

self.backGroundView.frame = CGRectMake(0, 0, kScreenwidth, 49);

self.frame = CGRectMake(0, kScreenheight-49, kScreenwidth, 49);

}else{

CGRect rect = CGRectMake(0, 0, kScreenwidth, self.backGroundView.frame.size.height);

self.backGroundView.frame = rect;

self.frame = CGRectMake(0, kScreenheight - rect.size.height, kScreenwidth, self.backGroundView.frame.size.height);

}

}

- (void)centerTapClick{

[self.textView resignFirstResponder];

}

#pragma mark --- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{

/**

*? 設(shè)置占位符

*/

if (textView.text.length == 0) {

self.placeholderLabel.text = placeholderText;

[self.sendButton setBackgroundColor:UIColorRGB(180, 180, 180)];

self.sendButton.userInteractionEnabled = NO;

}else{

self.placeholderLabel.text = @"";

[self.sendButton setBackgroundColor:UIColorRGB(70 , 163, 231)];

self.sendButton.userInteractionEnabled = YES;

}

//---- 計(jì)算高度 ---- //

CGSize size = CGSizeMake(kScreenwidth-65, CGFLOAT_MAX);

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];

CGFloat curheight = [textView.text boundingRectWithSize:size

options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading

attributes:dic

context:nil].size.height;

CGFloat y = CGRectGetMaxY(self.backGroundView.frame);

if (curheight < 19.094) {

statusTextView = NO;

self.backGroundView.frame = CGRectMake(0, y - 49, kScreenwidth, 49);

}else if(curheight < MaxTextViewHeight){

statusTextView = NO;

self.backGroundView.frame = CGRectMake(0, y - textView.contentSize.height-10, kScreenwidth,textView.contentSize.height+10);

}else{

statusTextView = YES;

return;

}

}

#pragma? mark -- 發(fā)送事件

- (void)sendClick:(UIButton *)sender{

[self.textView endEditing:YES];

if (self.EwenTextViewBlock) {

self.EwenTextViewBlock(self.textView.text);

}

//---- 發(fā)送成功之后清空 ------//

self.textView.text = nil;

self.placeholderLabel.text = placeholderText;

[self.sendButton setBackgroundColor:UIColorRGB(180, 180, 180)];

self.sendButton.userInteractionEnabled = NO;

self.frame = CGRectMake(0, kScreenheight-49, kScreenwidth, 49);

self.backGroundView.frame = CGRectMake(0, 0, kScreenwidth, 49);

}

#pragma mark --- 懶加載控件

- (UIView *)backGroundView{

if (!_backGroundView) {

_backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 49)];

_backGroundView.backgroundColor = UIColorRGB(230, 230, 230);

[self addSubview:_backGroundView];

}

return _backGroundView;

}

- (UITextView *)textView{

if (!_textView) {

_textView = [[UITextView alloc]init];

_textView.font = [UIFont systemFontOfSize:16];

_textView.delegate = self;

_textView.layer.cornerRadius = 5;

[self.backGroundView addSubview:_textView];

}

return _textView;

}

- (UILabel *)placeholderLabel{

if (!_placeholderLabel) {

_placeholderLabel = [[UILabel alloc]init];

_placeholderLabel.font = [UIFont systemFontOfSize:16];

_placeholderLabel.textColor = [UIColor grayColor];

[self.backGroundView addSubview:_placeholderLabel];

}

return _placeholderLabel;

}

- (UIButton *)sendButton{

if (!_sendButton) {

_sendButton = [[UIButton alloc]init];

[_sendButton setBackgroundColor:UIColorRGB(180, 180, 180)];

[_sendButton setTitle:@"發(fā)送" forState:UIControlStateNormal];

[_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[_sendButton addTarget:self action:@selector(sendClick:) forControlEvents:UIControlEventTouchUpInside];

_sendButton.layer.cornerRadius = 5;

_sendButton.userInteractionEnabled = NO;

[self.backGroundView addSubview:_sendButton];

}

return _sendButton;

}

#pragma mark --- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

if (statusTextView == NO) {

scrollView.contentOffset = CGPointMake(0, 0);

}else{

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沸移,一起剝皮案震驚了整個(gè)濱河市婆誓,隨后出現(xiàn)的幾起案子刑顺,更是在濱河造成了極大的恐慌隶校,老刑警劉巖夺姑,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異穆桂,居然都是意外死亡宫盔,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門享完,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灼芭,“玉大人,你說我怎么就攤上這事般又”吮粒” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵茴迁,是天一觀的道長寄悯。 經(jīng)常有香客問我,道長堕义,這世上最難降的妖魔是什么猜旬? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮倦卖,結(jié)果婚禮上洒擦,老公的妹妹穿的比我還像新娘。我一直安慰自己怕膛,他們只是感情好熟嫩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著褐捻,像睡著了一般掸茅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舍扰,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天倦蚪,我揣著相機(jī)與錄音,去河邊找鬼边苹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛裁僧,可吹牛的內(nèi)容都是我干的个束。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼聊疲,長吁一口氣:“原來是場噩夢啊……” “哼茬底!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起获洲,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤阱表,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體最爬,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涉馁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了爱致。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烤送。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖糠悯,靈堂內(nèi)的尸體忽然破棺而出帮坚,到底是詐尸還是另有隱情,我是刑警寧澤互艾,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布试和,位于F島的核電站,受9級特大地震影響纫普,放射性物質(zhì)發(fā)生泄漏灰署。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一局嘁、第九天 我趴在偏房一處隱蔽的房頂上張望溉箕。 院中可真熱鬧,春花似錦悦昵、人聲如沸肴茄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽寡痰。三九已至,卻和暖如春棋凳,著一層夾襖步出監(jiān)牢的瞬間拦坠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工剩岳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贞滨,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓拍棕,卻偏偏與公主長得像晓铆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子绰播,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容

  • (一)Masonry介紹 Masonry是一個(gè)輕量級的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動(dòng)布...
    木易林1閱讀 2,334評論 0 3
  • Masonry是一個(gè)輕量級的布局框架骄噪,擁有自己的描述語法,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動(dòng)布局蠢箩,簡潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,765評論 0 1
  • 一九三零年链蕊,東北遼寧事甜,一個(gè)破舊的火車站。晉商李茂平穿著樸素的灰馬褂滔韵,背著一個(gè)破布褡褳站在墻角逻谦,褡褳里面的東西看起來...
    點(diǎn)兜閱讀 816評論 1 5
  • 在青春的似水流年里齿兔,總有一些遺憾也總有一些慶幸橱脸,幸好我們都還只是孩子,需要的就是在追夢的路上不斷的成長分苇。所以我們可...
    余珂閱讀 519評論 3 3
  • 將人腦與計(jì)算機(jī)類比添诉,相信大家都已不再覺得新奇和陌生了。借助這個(gè)類比医寿,今天想聊聊影響我們?nèi)粘P袨闆Q策中的一個(gè)有趣話題...
    徐彥超閱讀 660評論 0 4