Snowman
Snowman

Reputation: 32081

Typing into UITextView with formatting in iOS 6

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.

Upvotes: 3

Views: 7562

Answers (3)

Smilin Brian
Smilin Brian

Reputation: 990

If I understand the question correctly, this is what the typingAttributes property of the UITextView is for.

That is, you'd want to do something like:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 50.f;
mainTextView.typingAttributes = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

Upvotes: 7

David Hoerl
David Hoerl

Reputation: 41682

The key to your problem is this delegate method:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

What you do with it is keep a private string or attributed string. When this method gets called, you will see the proposed new character(s) that should go in what position and possibly replacing some other characters. By using your private string, or a copy of textView.text, and modifying it, you know have the full string.

You can create a new attributed string or modify your existing one, set it on the textView:

textView.attributedString = myNewString;

then return NO. Modifying the old string is easy as there are already methods provided by NSString that you let change characters specified with a range by other characters.

Upvotes: 3

AliSoftware
AliSoftware

Reputation: 32681

You should try to use the delegate methods of UITextView to be notified when the use has entered text, then change the text attributes in this method.

Upvotes: 0

Related Questions