Well this seems silly that it can’t be done – you can set a predefined attributed string to be displayed on a text view with formatting, but you can’t set general formatting to be applied to live typing on a UITextView.
For example, if I do this before typing anything into the text view:
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;
Then formatting displays, and when I edit the text with the keyboard, formatting remains.
However, what if I want to start with a blank UITextView and apply formatting to anything the user will type into the view?
I tried setting string = @"" or string = @" ", but neither of those retain formatting. Is there really no way to apply formatting to user input? What’s the point then of a UITextView having an attributed string property?
According to the docs, setting the text property of the UITextView completely wipes off any formatting done by the attributed string. So whatever the solution is, it would have to not directly set textView.text property, but rather only tweak with the attributedText property.
If I understand the question correctly, this is what the
typingAttributesproperty of theUITextViewis for.That is, you’d want to do something like: