Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextview typingAttributes not working

I have UITextView and I want to set it's line height to 50.0f so I'm using typingAttributes, but nothing works, my code goes like this in ViewDidAppear Method

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight        = 50.0f;
paragraphStyle.lineHeightMultiple       = 50.0f;
paragraphStyle.maximumLineHeight        = 50.0f;

NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.typingAttributes  = textViewAttributeDic;

text doesn't effected by setting typingAttributes,and I tried to changed the color and font using typingAttributesbut nothing works

i've read all stack answers and documentation

what i'm doing wrong :(

update: i even tried

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight        = 50.0f;
paragraphStyle.lineHeightMultiple       = 50.0f;
paragraphStyle.maximumLineHeight        = 50.0f;

NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.attributedText  = [[NSAttributedString alloc] initWithString:@"" attributes:textViewAttributeDic];

when I tried

textView.attributedText  = [[NSAttributedString alloc] initWithString:@"blahblah" attributes:textViewAttributeDic];

It worked, but i need empty textView with no spaces or 'blah' characters

like image 447
Homam Avatar asked Mar 15 '26 21:03

Homam


2 Answers

The documentation clearly states that typingAttributes is for the editing mode of the text field.

typingAttributes

The attributes to apply to new text being entered by the user.

... If the text field is not in editing mode, this property contains the value nil. Similarly, you cannot assign a value to this property unless the text field is currently in editing mode.

Instead, you should assign attributedText instead of the text property. The mechanism to specify the attributes is via NSAttributedString that you assign.

like image 106
Mundi Avatar answered Mar 17 '26 12:03

Mundi


Programmatically setting attributedText resets the typingAttributes. Set typingAttributes last.

like image 29
Ricardo Sanchez-Saez Avatar answered Mar 17 '26 13:03

Ricardo Sanchez-Saez