中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

UITextView 限制字符長度

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
頁面啟動時添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChange:) name:@"UITextViewTextDidChangeNotification" object:self.txtSuggestion];
 
 
 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (toBeString.length > kMaxLength && range.length!=1){
        textView.text = [toBeString substringToIndex:kMaxLength];
        return NO;
    }
    return YES;
}
 
-(void)textViewChange:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
     
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; // 鍵盤輸入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 簡體中文輸入,包括簡體拼音,健體五筆,簡體手寫
        UITextRange *selectedRange = [textField markedTextRange];
        //獲取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 沒有高亮選擇的字,則對已輸入的文字進(jìn)行字?jǐn)?shù)統(tǒng)計和限制
        if (!position) {
            if (toBeString.length > kMaxLength) {
                textField.text = [toBeString substringToIndex:kMaxLength];
            }
        }
        // 有高亮選擇的字符串,則暫不對文字進(jìn)行統(tǒng)計和限制
        else{
             
        }
    }
    // 中文輸入法以外的直接對其統(tǒng)計限制即可,不考慮其他語種情況
    else{
        if (toBeString.length > kMaxLength) {
            textField.text = [toBeString substringToIndex:kMaxLength];
        }  
    }  
}
 
 
離開頁面時
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"UITextFieldTextDidChangeNotification"  
object:self.txtSuggestion];

標(biāo)簽: idc

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:JS 實(shí)現(xiàn)快速冪算法

下一篇:C# Access數(shù)據(jù)庫操作