Reputation: 568
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
Upvotes: 3
Views: 8390
Reputation: 1
I took a hint from @Mundi's answer.
In my case, I set typingAttributes
when the viewController was created, and it did not work
I was entering code according to placeholder in textViewShouldBeginEditing
, so I reset typingAttributes
in that method, but it didn't work the same way.
When I saw @Mundi's answer and resetting typingAttributes
in textViewDidBeginEditing
, it worked fine.
Upvotes: 0
Reputation: 26536
Here's one gotcha - as mentioned in other answers, the UITextField
must be in editing mode for this to work. If you are running in the simulator, and the keyboard is hidden (cmd+k), the textfield is not in editing mode if you type using your computer's keyboard. Make sure the simulator's keyboard is displayed.
Upvotes: 2
Reputation: 9566
Programmatically setting attributedText
resets the typingAttributes
. Set typingAttributes
last.
Upvotes: 4
Reputation: 568
in ios6 I solved it like this , in textView delegate I wrote:
- (void)textViewDidChange:(UITextView *)textView{
// 1st save current selected range ... we gonna use this value in 5th step
NSRange textViewCurrentRange = textView.selectedRange;
// 2nd disable scrolling in textview
[textView setScrollEnabled:NO];
// 3rd set the new enterd text as attributed string to textview
[textView setAttributedText:[[NSAttributedString alloc]initWithString:textView.text attributes:self.textAttributes]];
// 4th enable scrolling
[textView setScrollEnabled:YES];
// 5th re set selected range so text inidicator will get back to it's place
textView.selectedRange = textViewCurrentRange;
}
Upvotes: 2
Reputation: 10323
You may configure paragraph style in IB using temporary text in text view and setting all required attributes. Then you clear your text view on launch textView.text = nil
. Text attributes should remain the same.
Upvotes: 3
Reputation: 80271
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.
Upvotes: 4