Swift UITextView 限制輸入字元數

2021-08-10 13:16:37 字數 1452 閱讀 8493

我們經常遇到統計字元數的問題呢,限制使用者輸入,但是聯想詞很難處理,這裡就提供一種方法處理聯想詞,讓字元的統計和輸入很完美實現。

//**

//初始化uitextview

self

.textview = [[uitextview alloc] initwithframe:cgrectmake(15*uirate, 5*uirate, screenwidth - 30*uirate, 130*uirate)];

self

.textview

self

.textview

.placeholdercolor = [uicolor fontcolorlightgray];

self

.textview

.textcolor = [uicolor fontcolorblack];

self

.textview

.font = font_system(15);

_textview.delegate = self;

[holdview addsubview:self

.textview];

//實現**方法

//total_num 200

- (void)textviewdidchange:(uitextview *)textview

nsstring *textcontent = textview.text;

int textnum = (int)textcontent.length;

//擷取200個字

if (textnum > total_num)

else}}

self

.textnum = (int)textview.text

.length;

self

.numlabel

.text = [nsstring stringwithformat:@"%d/%d",self

.textnum, total_num];

}

初始化等見oc版本

//mark: - textviewdelegate

func textviewdidchange(_ textview: uitextview)

}let textcontent = textview.text

let textnum = textcontent?.characters.count

//擷取200個字

if textnum! > total_num

}self.textnum = textview.text.characters.count

self.numlabel.text = "(\(self.textnum)/\(total_num))"

}

C 輸入字元限制

只能輸入數字 0 9 只能輸入 n 位的數字 d 只能輸入至少 n 位的數字 d 只能輸入 m n 位的數字 d 只能輸入零和非零開頭的數字 0 1 9 0 9 只能輸入有兩位小數的正實數 0 9 0 9 只能輸入有 1 3 位小數的正實數 0 9 0 9 只能輸入非零的正整數 1 9 0 9 只能...

C 輸入字元限制

只能輸入數字 1 katex parse error expected group after at position 17 只能輸入 n 位的數字 d 只能輸入至少 n 位的數字 d 只能輸入 m n 位的數字 d 只能輸入零和非零開頭的數字 0 1 9 0 9 只能輸入有兩位小數的正實數 2 0...

限制輸入特殊字元

有時候我們輸入一些東西,或者發表一些文章,會提示,您輸入的內容函式特殊字元,或者不正常的字眼,這樣的功能是怎麼實現的呢?本菜鳥找到了乙個限制特殊字元輸入小功能,現在分享給大家,希望對大家在後期的學習中,有幫助。自定義方法 自定義要檢查的字元 private sub specialcheck stri...