textview進行編輯是中文后面接英文時,會自動換行,導(dǎo)致同一行顯示不滿。
解決方法如下:
- (void)textViewDidChange:(UITextView *)textView {
//防止輸入時在中文后輸入英文過長直接中文和英文換行
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:14],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
換行問題解決了,那么新的問題來了,系統(tǒng)中文鍵盤輸入時,中文輸入時拼音會先一步顯示到文本(這里是我還沒選擇中文輸入內(nèi)容,拼音就亂入了)
解決方法還是放在textViewDidchange里面
///防止拼音輸入時,文本直接獲取拼音
UITextRange *selectedRange = [textView markedTextRange];
NSString * newText = [textView textInRange:selectedRange];? ?? //獲取高亮部分
if(newText.length>0)
{
return;
}
以上方法同樣適用于TextField。