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

UITextView 限制字符長(zhǎng)度

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
頁面啟動(dòng)時(shí)添加通知
[[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"]) { // 簡(jiǎn)體中文輸入,包括簡(jiǎn)體拼音,健體五筆,簡(jiǎn)體手寫
        UITextRange *selectedRange = [textField markedTextRange];
        //獲取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 沒有高亮選擇的字,則對(duì)已輸入的文字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制
        if (!position) {
            if (toBeString.length > kMaxLength) {
                textField.text = [toBeString substringToIndex:kMaxLength];
            }
        }
        // 有高亮選擇的字符串,則暫不對(duì)文字進(jìn)行統(tǒng)計(jì)和限制
        else{
             
        }
    }
    // 中文輸入法以外的直接對(duì)其統(tǒng)計(jì)限制即可,不考慮其他語種情況
    else{
        if (toBeString.length > kMaxLength) {
            textField.text = [toBeString substringToIndex:kMaxLength];
        }  
    }  
}
 
 
離開頁面時(shí)
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"UITextFieldTextDidChangeNotification"  
object:self.txtSuggestion];

標(biāo)簽: idc

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

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

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