Search code examples
iphoneobjective-cuitextviewnsattributedstring

How to set Line Spacing in UI Text View (Empty text view) to enter message with line space


In my app i need to set some line spacing on ui text view..

I know we can do it for non editable textviews / labels using paragraph style spacing

But in my app when i enter text it was not working,

I can do it only when i have a predefined text on it, if once i clear the text paragraph sty will not work

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

    UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:18.f];
    NSString *string = @"This is a test";
    NSDictionary *attributtes = @{
                                  NSParagraphStyleAttributeName : paragraphStyle,
                                  };
    deedTextView.font = font;
    deedTextView.attributedText = [[NSAttributedString alloc] initWithString:string
                                                                   attributes:attributtes];

But, I dont have any pre defined text like NSString *string = @"This is a test";

Text view must be empty, while begin


Solution

  • I had the same Problem. Based on Sergius answer I came up with the following working solution. The problem with Sergius answer was that all other already set attributes will be overwritten (Font, Color...)

    So it is better to edit the existing typingAttributes:

    NSDictionary* d = deedTextView.typingAttributes;
    NSMutableDictionary* md = [NSMutableDictionary dictionaryWithDictionary:d];
    [md setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    deedTextView.typingAttributes= md;