简体   繁体   中英

NSTextField with condensed font line spacing…

I'm trying to understand how to properly display text with condensed line spacing in a text field. When I set paragraph style properties lineHeightMultiple, maximumLineHeight, and minimumLineHeight I can achieve the effect of condensing the lines, but one side effect is that the top line of text just gets clipped off. So I thought that I'd just be able to move the text down with NSBaselineOffsetAttributeName (using a negative value), but that doesn't seem to have any effect. I'm using a line height here of 70% of the point size, but the clipping gets far worse the more condensed it gets.

1) Is there a better way to produce a condensed font line spacing? 2) Or how would you move the text rendering downward so it doesn't get clipped.

<update>

Ok my answer below does address a solution when using NSTextField's. But this obviously doesn't work for NSTextView's too. I tired to override the baselineOffset in the NSLayoutManagerDelegate's shouldSetLineFragmentRect... method, but it also ignores baseline adjustments. Anyone have any suggestions when working with the NSTextView?

</update>

Thanks!

Here's the test project I'm working with https://www.dropbox.com/s/jyshqeuirujf71g/WhatThe.zip?dl=0

用裁剪的顶部文字进行渲染

Codez:

self.label.wantsLayer = YES;
self.label.backgroundColor = [NSColor whiteColor];
self.label.hidden = NO;
self.label.maximumNumberOfLines = 0;

NSMutableDictionary *result = [NSMutableDictionary dictionary];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];

NSFont *font = [NSFont systemFontOfSize:80.0f];

CGFloat lineHeight = font.pointSize * .7f;
CGFloat natualLineHeight = font.ascender + ABS(font.descender) + font.leading;

paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineHeightMultiple = lineHeight / natualLineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.paragraphSpacing = 0.0f;
paragraphStyle.allowsDefaultTighteningForTruncation = paragraphStyle.lineBreakMode != NSLineBreakByWordWrapping && paragraphStyle.lineBreakMode != NSLineBreakByCharWrapping && paragraphStyle.lineBreakMode != NSLineBreakByClipping;

result[NSParagraphStyleAttributeName] = paragraphStyle;
result[NSKernAttributeName] = @(0.0f);
result[NSBaselineOffsetAttributeName] = @(-50.0f);

result[NSFontAttributeName] = font;

result[NSForegroundColorAttributeName] = [NSColor blackColor];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello\nThere" attributes:result];
self.label.attributedStringValue = attributedString;

Ok. By subclassing NSTextFieldCell I was able to offset the text correctly. It's a shame that this method works nicely in iOS-land. Maybe this will work when the unified Mac/iOS UI APIs are released this summer. 😁

This will remove any negative baseline values from the string before it draws and draw inside a shifted rect.

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
NSMutableAttributedString *string = [self.attributedStringValue mutableCopy];

__block CGFloat baselineOffset = 0.0f;

[string enumerateAttributesInRange:NSMakeRange(0, string.length) options:0 usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {

    NSNumber *offsetValue = attrs[NSBaselineOffsetAttributeName];
    if (offsetValue != nil && offsetValue.floatValue < 0.0f) {
        baselineOffset = MIN(baselineOffset, offsetValue.floatValue);
        [string removeAttribute:NSBaselineOffsetAttributeName range:range];
    }
}];

titleRect.origin.y -= baselineOffset;

[string drawInRect:titleRect];
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM