如何正确确定属性字符串的宽度

发布于 2024-09-17 07:15:16 字数 3091 浏览 5 评论 0原文

我想要做的:使用 NSLayoutManager 布局文本并将其 NSTextContainer 设置为字符串数组中最宽字符串(字形)的宽度。

我的问题是:确定总“字形宽度”的方法似乎不正确,因为当我渲染文本时它会换行。

我使用 Monaco 12 点字体的 32 个字符的字符串做了一个实验,长度报告为 224.0,但只有当长度设置为 234.0 时,文本才会停止换行。

此代码演示了我上面所说的内容,并在计算的字形宽度右侧显示一条垂直线。

- (void)drawRect:(NSRect)rect {
    NSRect bounds = [self bounds];

    [[NSColor whiteColor] drawSwatchInRect: bounds];
    [[NSColor blackColor] setStroke];
    [[NSBezierPath bezierPathWithRect: bounds] stroke];

    NSMutableParagraphStyle *thisParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    [thisParagraphStyle setLineBreakMode: NSLineBreakByCharWrapping];
    [thisParagraphStyle setAlignment: NSLeftTextAlignment];

    NSFont *fontUsed = [NSFont fontWithName: @"Monaco" size: 12];

    NSDictionary *glyphAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     fontUsed, NSFontAttributeName,
                                     thisParagraphStyle, NSParagraphStyleAttributeName,
                                     [NSColor blackColor], NSForegroundColorAttributeName,
                                     NULL];

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n"];

    [textStorage setAttributes: glyphAttributes range:  NSMakeRange(0, [textStorage length])];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    NSSize textContainerSize;
    textContainerSize.width = [@"00112233445566778899001122334455" sizeWithAttributes: glyphAttributes].width;
    textContainerSize.height = bounds.size.height;

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize: textContainerSize];
    [layoutManager addTextContainer:textContainer];

    [textContainer release];
    [textStorage addLayoutManager:layoutManager];
    [layoutManager release];
    NSRange glyphRange = [layoutManager glyphRangeForTextContainer: textContainer];

    [layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: NSMakePoint(0.0 , 0.0)];
    [textStorage release];
    [thisParagraphStyle release];

    // Indicate right text boundary from computed width
    NSBezierPath *rightTextBoundary = [NSBezierPath bezierPath];
    [rightTextBoundary moveToPoint: NSMakePoint(textContainerSize.width, 0.0)];
    [rightTextBoundary lineToPoint: NSMakePoint(textContainerSize.width, bounds.size.height-1)];
    [rightTextBoundary stroke];

    NSLog(@"View width: %f", bounds.size.width);
    NSLog(@"Calculated width1: %f", textContainerSize.width);
    NSLog(@"Calculated width2: %f\n\n", [@"00112233445566778899001122334455" boundingRectWithSize: NSMakeSize(FLT_MAX, FLT_MAX)
                                                                         options: NSStringDrawingUsesDeviceMetrics
                                                                      attributes: glyphAttributes].size.width);
}

- (BOOL) isFlipped {
    return YES;
}

What I want to do: layout text using NSLayoutManager and set its NSTextContainer to the width of the widest string (glyph-wise) in an array of strings.

What my problem is: The methods for determining the total 'glyph-width' seem to be incorrect because when I render the text it wraps.

I did an experiment using a 32 character string with the Monaco 12 point font and the length is reported as 224.0, but the text will only stop wrapping if the length is set to 234.0.

This code demonstrates what I said above and shows a vertical line on the right of the computed glyph-width.

- (void)drawRect:(NSRect)rect {
    NSRect bounds = [self bounds];

    [[NSColor whiteColor] drawSwatchInRect: bounds];
    [[NSColor blackColor] setStroke];
    [[NSBezierPath bezierPathWithRect: bounds] stroke];

    NSMutableParagraphStyle *thisParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    [thisParagraphStyle setLineBreakMode: NSLineBreakByCharWrapping];
    [thisParagraphStyle setAlignment: NSLeftTextAlignment];

    NSFont *fontUsed = [NSFont fontWithName: @"Monaco" size: 12];

    NSDictionary *glyphAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     fontUsed, NSFontAttributeName,
                                     thisParagraphStyle, NSParagraphStyleAttributeName,
                                     [NSColor blackColor], NSForegroundColorAttributeName,
                                     NULL];

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n"];

    [textStorage setAttributes: glyphAttributes range:  NSMakeRange(0, [textStorage length])];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    NSSize textContainerSize;
    textContainerSize.width = [@"00112233445566778899001122334455" sizeWithAttributes: glyphAttributes].width;
    textContainerSize.height = bounds.size.height;

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize: textContainerSize];
    [layoutManager addTextContainer:textContainer];

    [textContainer release];
    [textStorage addLayoutManager:layoutManager];
    [layoutManager release];
    NSRange glyphRange = [layoutManager glyphRangeForTextContainer: textContainer];

    [layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: NSMakePoint(0.0 , 0.0)];
    [textStorage release];
    [thisParagraphStyle release];

    // Indicate right text boundary from computed width
    NSBezierPath *rightTextBoundary = [NSBezierPath bezierPath];
    [rightTextBoundary moveToPoint: NSMakePoint(textContainerSize.width, 0.0)];
    [rightTextBoundary lineToPoint: NSMakePoint(textContainerSize.width, bounds.size.height-1)];
    [rightTextBoundary stroke];

    NSLog(@"View width: %f", bounds.size.width);
    NSLog(@"Calculated width1: %f", textContainerSize.width);
    NSLog(@"Calculated width2: %f\n\n", [@"00112233445566778899001122334455" boundingRectWithSize: NSMakeSize(FLT_MAX, FLT_MAX)
                                                                         options: NSStringDrawingUsesDeviceMetrics
                                                                      attributes: glyphAttributes].size.width);
}

- (BOOL) isFlipped {
    return YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-09-24 07:15:17

您是否考虑过文本容器的 -lineFragmentPadding

Have you accounted for the text container's -lineFragmentPadding?

话少心凉 2024-09-24 07:15:17

您是否看过 NSString 和 NSAttributedString NS(Attributed)String+Geometrics

如果这不完全是您正在寻找的内容,您也许可以看到他们为计算大小所做的一些事情。

Have you taken a look at this category for NSString and NSAttributedString NS(Attributed)String+Geometrics?

If it's not exactly what you're looking for you could perhaps see some of what they are doing to calculate the size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文