TextView 限制字?jǐn)?shù)

實(shí)現(xiàn)TextView提示文字并且輸入字?jǐn)?shù)限制的反饋意見功能-----非常簡(jiǎn)潔明了!如圖實(shí)現(xiàn)頁面,效果如下:

1.輸入文字的時(shí)候提示文字消失,TextView沒有文字的時(shí)候提示文字顯示;

2.右下角實(shí)時(shí)顯示字?jǐn)?shù);

3.字?jǐn)?shù)到達(dá)指定限制后,TextView不能輸入更多,可以刪除;

4.提交按鈕在TextView不為空的時(shí)候按鈕為綠色且可點(diǎn)擊;TextView為空時(shí),為灰色狀態(tài)且不可點(diǎn)擊。

.m中的代碼如下:

#import"YJFeedBackViewController.h"

@interfaceYJFeedBackViewController()

@property(weak,nonatomic)IBOutletUILabel*placeHolder;

@property(weak,nonatomic)IBOutletUIButton*commitButton;

@property(weak,nonatomic)IBOutletUITextView*feedBackTextView;

@property(weak,nonatomic)IBOutletUILabel*stirngLenghLabel;

@end

@implementationYJFeedBackViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.title=@"反饋意見";

self.automaticallyAdjustsScrollViewInsets=NO;

self.feedBackTextView.delegate=self;

self.placeHolder.userInteractionEnabled=NO;

self.commitButton.userInteractionEnabled=NO;

self.feedBackTextView.layer.borderWidth=0.5;

self.feedBackTextView.layer.borderColor= [UIColorlightGrayColor].CGColor;

// Do any additional setup after loading the view from its nib.

}

//正在改變

- (void)textViewDidChange:(UITextView*)textView

{

FDLog(@"%@", textView.text);

self.placeHolder.hidden=YES;

//允許提交按鈕點(diǎn)擊操作

self.commitButton.backgroundColor=FDMainColor;

self.commitButton.userInteractionEnabled=YES;

//實(shí)時(shí)顯示字?jǐn)?shù)

self.stirngLenghLabel.text= [NSStringstringWithFormat:@"%lu/100", (unsignedlong)textView.text.length];

//字?jǐn)?shù)限制操作

if(textView.text.length>=100) {

textView.text= [textView.textsubstringToIndex:100];

self.stirngLenghLabel.text=@"100/100";

}

//取消按鈕點(diǎn)擊權(quán)限,并顯示提示文字

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

self.placeHolder.hidden=NO;

self.commitButton.userInteractionEnabled=NO;

self.commitButton.backgroundColor= [UIColorlightGrayColor];

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • UITextView文本輸入這個(gè)常用也不太常用的控件,它本身已經(jīng)有了不少功能,但是相對(duì)UITextField自帶有...
    潔簡(jiǎn)閱讀 1,558評(píng)論 0 2
  • 看到各種大神的帖子,小弟我一時(shí)手癢,正好前不久因?yàn)轫?xiàng)目的需要,就弄了這么一個(gè)玩意,希望能幫助大家。 剛開始做這個(gè)的...
    跟上時(shí)代的腳步閱讀 5,478評(píng)論 0 1
  • 占位文字1、曾經(jīng)有個(gè)這么一個(gè)項(xiàng)目需求: 使用textField時(shí),占位文字默認(rèn)是黑色的,我們的需求是當(dāng)開始編輯時(shí),...
    博行天下閱讀 1,023評(píng)論 2 1
  • 南風(fēng)輕挪欲滴露,未了池緣今又園。 枝枝亭亭顆顆玉,散落風(fēng)塵呢喃雨。 雨停風(fēng)住世間路,落得行人踟躕語。 此處難能比瑤...
    沒石雨閱讀 185評(píng)論 0 0
  • 冉閔戰(zhàn)敗被俘后,慕容儁大喜過望,派弟弟慕容評(píng)和侯龕率領(lǐng)一萬精銳騎兵直奔魏國都城鄴城。 城中已經(jīng)無人可以領(lǐng)兵出戰(zhàn),將...
    寒七琪閱讀 442評(píng)論 0 1

友情鏈接更多精彩內(nèi)容