Search code examples
iosuitextviewnsattributedstring

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


Solution

  • 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;
     }