CoreText & NSMutableParagraphStyle maximumLineHeight
CoreText & NSMutableParagraphStyle maximumLineHeight
- Subject: CoreText & NSMutableParagraphStyle maximumLineHeight
- From: Peter C <email@hidden>
- Date: Tue, 20 Aug 2013 12:08:23 +0800
Hi,
I am attempting to create text control something similar like MS Word "line spacing" feature. A user complain my App paragraph text lines space is too much, this is because it is done by using NSString drawInRect:withAttributes:, not much of any adjustment. CoreText seems to able to do so by setting minimum and maximum line height.
maximumLineHeight when set to 1.0, the lines is maximum compacted to unreadable text. Gradual increase of the value, will see the lines expanding. However I discover it to be strange behaviour of maximumLineHeight (probably something I don't understand yet). maximumLineHeight does not correspond to font size for some font types. For some other fonts, maximumLineHeight is equivalent to font size. I try calculating from from font ascent, descent and leading value, it just does not add up.
For example, for Helvetica font size of 12.0, maximumLineHeight is 15.0 to equal as maximumLineHeight = 0.
I don't understand where does the extra 3.0 value come from ? Is it possible to retrieve from NSMutableAttributedString or from CTFramesetter functions ?
I hope some one could enlighten me on this, have knocking my head for past few days.
Below is the snippet for example, font size is set to 12.0,
>> It will take maximumLineHeight = 15 to be equal as maximumLineHeight = 0 for Helvetica
[self drawColumnText:<long text> font:@"Helvetica" size:12.0 rect:textRect maxHeight:<height>];
>> It will take maximumLineHeight = 14 to be equal as maximumLineHeight = 0 for ArialMT
[self drawColumnText:<long text> font:@"ArialMT" size:12.0 rect:textRect maxHeight:<height>];
>> It will take maximumLineHeight = 14 to be equal as maximumLineHeight = 0 for Courier
[self drawColumnText:<long text> font:@"Courier" size:12.0 rect:textRect maxHeight:<height>];
>> It will take maximumLineHeight = 12 to be equal as maximumLineHeight = 0 for HoboStd
[self drawColumnText:<long text> font:@"HoboStd" size:12.0 rect:textRect maxHeight:<height>];
>> It will take maximumLineHeight = 12 to be equal as maximumLineHeight = 0 for Herculanum
[self drawColumnText:<long text> font:@"Herculanum" size:12.0 rect:textRect maxHeight:<height>];
- (void)drawColumnText:(NSString *)drawStr font:(NSString *)fontNameStr size:(CGFloat)fontSize rect:(NSRect)textRect maxHeight:(CGFloat)maxLineHeight
{
CGContextRef context;
CTFontRef fontRef;
CGMutablePathRef path;
NSMutableAttributedString *attString;
NSMutableParagraphStyle *paragraphStyle;
NSDictionary *attDictionary;
context = [[NSGraphicsContext currentContext] graphicsPort];
fontRef = CTFontCreateWithName((CFStringRef)fontNameStr, fontSize, NULL);
attDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
(id)fontRef, (NSString *)kCTFontAttributeName,
[NSColor blueColor], (NSString *)kCTForegroundColorAttributeName,
nil];
attString = [[NSMutableAttributedString alloc] initWithString:drawStr attributes:attDictionary];
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// ************* maximum Line height ***********
paragraphStyle.maximumLineHeight = maxLineHeight; // affects spacing
[attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attString length])];
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
path = CGPathCreateMutable();
CGPathAddRect(path, NULL, textRect);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);
CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attString length]), path, NULL);
[self measureFrame:theFrame];
CFRelease(framesetter);
CFRelease(path);
CTFrameDraw(theFrame, context);
CFRelease(theFrame);
[paragraphStyle release];
[attString release];
CFRelease(fontRef);
}
Peter Chan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden