Search code examples
iphoneiosuitextviewcore-textcss

LineHeight in EGOTextView


Im employing EGOTextView to do rich text editing. But the lineHeight of the EGOTextView appears a little smaller than in the UITextView, which are set the same font. I have tried to set the kCTParagraphStyleSpecifierMinimumLineHeight and kCTParagraphStyleSpecifierMaximumLineHeight in the default attribute, but it is not a good solution as I need to insert image which has to modify the lineHeight. Any help will be appreciated:)

enter image description hereenter image description here

EGOTextView.m

- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"UITextView", @"EGOTextView", nil]];
segment.segmentedControlStyle = UISegmentedControlStyleBar;
[segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segment;
[segment release];

if (_textView==nil) {
    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    textView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:textView];
    self.textView = textView;
    [textView release];
}

if (_egoTextView==nil) {
    EGOTextView *view = [[EGOTextView alloc] initWithFrame:self.view.bounds];
    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    view.delegate = (id<EGOTextViewDelegate>)self;
    [self.view addSubview:view];
    self.egoTextView = view;
    self.egoTextView.delegate = self;
    [view release];  
    [self.egoTextView setFont:[UIFont systemFontOfSize:14]];
}
[segment setSelectedSegmentIndex:1];
}

Solution

  • You can set the kCTParagraphStyleSpecifierLineSpacing on the CTParagraphStyleSetting for the NSAttributedString to the number of points you would like to separate lines by. Alternatively, you could also specify a line height multiple using kCTParagraphStyleSpecifierLineHeightMultiple, for increasing line height by a factor.

    Here's a quick example:

    CGFloat lineSpacing = 0.f; // spacing, in points
    CGFloat lineHeightMultiple = 0.f; // line height multiple
    
    CTParagraphStyleSetting paragraphStyles[2] = {
        {.spec = kCTParagraphStyleSpecifierLineSpacing, .valueSize = sizeof(CGFloat), .value = (const void *)&lineSpacing},
        {.spec = kCTParagraphStyleSpecifierLineHeightMultiple, .valueSize = sizeof(CGFloat), .value = (const void *)&lineHeightMultiple}
    };
    
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphStyles, 10);
    [NSDictionary dictionaryWithObject:(__bridge id)paragraphStyle
                                forKey:(NSString *)kCTParagraphStyleAttributeName];
    CFRelease(paragraphStyle);