uitextview在中文后输入英文到行尾的时候,中文后的英文全部换到下一行,如下图所示:

会出现自动换行的问题,当然我们正常输入的时候是不希望看到这种情况的,所以,要去掉这种情况,我们只需在textView的代理方法- (void)textViewDidChange:(UITextView *)textView;中实现如下代码即可
- (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];
}
这样处理后就会看到效果如下图所示:

网友评论