xcode NSString sizeWithAttributes: 内容矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1992950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:01:59  来源:igfitidea点击:

NSString sizeWithAttributes: content rect

cocoaxcodemacosnsstring

提问by Alexandre Cassagne

How do I get a NSString's size as if it drew in an NSRect. The problem is when I try -[NSString sizeWithAttributes:], it returns an NSSize as if it had infinite width. I want to give a maximum width to the method. Is there any way of doing so? (BTW: Mac OS, not iPhone OS)

我如何获得 NSString 的大小,就像它是在 NSRect 中绘制的一样。问题是当我尝试 -[NSString sizeWithAttributes:] 时,它返回一个 NSSize,就好像它有无限宽度一样。我想为该方法提供最大宽度。有没有办法这样做?(顺便说一句:Mac 操作系统,而不是 iPhone 操作系统)

Thanks, Alex

谢谢,亚历克斯

回答by Alexandre Cassagne

float heightForStringDrawing(NSString *myString, NSFont *myFont,
        float myWidth)
{
 NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease];
 NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth, FLT_MAX)] autorelease];
 ;
        NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease];
 [layoutManager addTextContainer:textContainer];
 [textStorage addLayoutManager:layoutManager];
 [textStorage addAttribute:NSFontAttributeName value:myFont
      range:NSMakeRange(0, [textStorage length])];
 [textContainer setLineFragmentPadding:0.0];

 (void) [layoutManager glyphRangeForTextContainer:textContainer];
 return [layoutManager
   usedRectForTextContainer:textContainer].size.height;
}

It was in the docs after all. Thank you Joshua anyway!

毕竟它在文档中。无论如何,谢谢约书亚!

回答by ma11hew28

I revised Alexandre Cassagne's answerfor iOS with ARC enabled.

我在启用 ARC 的情况下修改了Alexandre Cassagne对 iOS的回答

CGSize ACMStringSize(NSString *string, UIFont *font, CGSize size)
{
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:string];
    [textStorage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [textStorage length])];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];
    return [layoutManager usedRectForTextContainer:textContainer].size;
}

回答by ingconti

for OSX cocoa/Swift 4: (using a custom NSview, to show also how to draw)

对于 OSX cocoa/Swift 4:(使用自定义 NSview,还展示如何绘制)

//
//  CustomView.swift
//  DrawMyString
//
//  Created by ing.conti on 19/10/2018.
//  Copyright ? 2018 com.ingconti. All rights reserved.
//

import Cocoa

class CustomView: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        let s : NSString = "AA BB CC DD EE FF"
        let W = 50
        let size = NSSize(width: W, height: 100)

        let boundingRectWithSize  = s.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [:])

        let r = NSRect(origin: CGPoint(x: 20, y: 30), size: boundingRectWithSize.size)

        s.draw(in: r, withAttributes: [:])
        r.frame()
    }
}

(we also draw border, to see it)

(我们也画边框,看看吧)

回答by Joshua Nozzi

I believe your only option here is NSLayoutManager and asking for a union of the used rects for a given glyph range.

我相信你在这里唯一的选择是 NSLayoutManager 并要求为给定的字形范围合并使用的矩形。