好的,这似乎很荒谬,它不能实现 - 您可以设置预定义的属性字符串以显示具有格式的文本视图,但您无法设置通用格式以应用于在 UITextView
上进行的实时输入。
例如,如果我在文本视图中输入任何内容之前执行此操作:
NSString *string = @"Hello world";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 50.f;
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:string];
[aStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[string length])];
mainTextView.attributedText = aStr;
然后就会出现格式化的显示,当我用键盘编辑文本时,格式化仍然存在。
但是,如果我想从一个空白的
UITextView
开始,并对用户输入到视图中的任何内容应用格式怎么办?我尝试设置
string = @""
或string = @" "
,但这两者都不能保留格式。难道没有办法应用格式来处理用户的输入吗?那么UITextView具有属性字符串属性的意义是什么?根据文档,设置
UITextView
的text
属性会完全清除属性字符串所做的任何格式化操作。因此,无论解决方案是什么,它都不能直接设置textView.text
属性,而只能调整attributedText
属性。
attributedText
会重置typingAttributes
。请最后设置typingAttributes
。 - Ricardo Sanchez-Saez