简体   繁体   English

自动调整NSTextView的大小及其字体大小

[英]Autoresizing NSTextView and it's Font Size

I'm trying to make my NSWindow autoresizable. 我正在尝试使我的NSWindow可自动调整大小。 I've gotten most of my UI items to resize, but one of the few remaining objects that pose issues are NSTextViews: indeed, I can't find a way to calculate the new font size once the NSTextView has been resized. 我已经调整了大部分UI项目,但剩下的几个引起问题的对象之一就是NSTextViews:确实,一旦调整了NSTextView的大小,我就找不到计算新字体大小的方法。

For NSTextFields, I had found a method that would find the font size based on the length of the text. 对于NSTextFields,我找到了一种方法,该方法可以根据文本的长度查找字体大小。 Apparently, there doesn't seem to be an equivalent method for multi-line text containers (unless I just haven't found it). 显然,多行文本容器似乎没有等效的方法(除非我只是没有找到它)。

The only actual place I found that mentionned something of the sort is here: http://boutcher.tumblr.com/post/7842216960/nstextview-auto-resizing-text 我发现的唯一实际的地方是在这里提到的东西: http : //boutcher.tumblr.com/post/7842216960/nstextview-auto-resizing-text

However, I wasn't able to implement this code into my application, as there seems to be an error I can't fix with the way an NSLayoutManager is created. 但是,我无法在我的应用程序中实现此代码,因为似乎无法解决NSLayoutManager的创建方式。

Has anyone done this in the past? 过去有人这样做吗? I'm considering just allowing the user to resize to just one size, so I can hardcode the font size... It's a real pain dealing with these NSTextViews ! 我只考虑允许用户调整到只有一个尺寸,这样我就可以硬编码的字体大小...这是处理这些NSTextViews一个真正的痛苦!

See the sizeWithAttributes: method in “NSString Application Kit Additions Reference.” 请参见“ NSString应用程序套件添加参考”中的sizeWithAttributes:方法。

It returns an NSSize, which you can compare to the textview's current frame.size. 它返回一个NSSize,您可以将其与文本视图的当前frame.size进行比较。

For the “Attributes” arg make an NSDictionary with an NSFont as the object and NSFontAttributeName as the key. 对于“属性”,使用NSFont作为对象,并以NSFontAttributeName作为键,创建一个NSDictionary。 (Don't be confused by that constant. It looks like it's a key for a string, but it is not; it is a key for the font itself.) (不要被该常量所迷惑。它看起来像是字符串的键,但不是;它是字体本身的键。)

Get the string from the textview: [[yourTextView textStorage] string] . 从textview获取字符串: [[yourTextView textStorage] string] Get the familyName of the font you are using and its current point size, a CGFloat. 获取您正在使用的字体的familyName及其当前磅值CGFloat。 Compose fonts to test using the constructor [NSFont fontWithName:familyName size:floatChanged] . 使用构造函数[NSFont fontWithName:familyName size:floatChanged]编写字体以进行测试。

Once you've arrived at the correctly sized font, use it to make a new NSAttributedString out of the old string. 一旦找到正确大小的字体,就可以使用它从旧字符串中生成一个新的NSAttributedString。 Just use the "attributes" dictionary you made above (the one that produced the correct size) and feed it to NSAttributedString's initWithString:attributes constructor. 只需使用上面制作的“属性”字典(产生正确大小的字典),并将其输入NSAttributedString的initWithString:attributes构造函数即可。

Assign that attributed string to the textStorage (itself a subclass of NSMutableAttributedString): [[yourTextView textStorage] setAttributedString:thatYouJustMade] . 将该属性字符串分配给textStorage(它本身是NSMutableAttributedString的子类): [[yourTextView textStorage] setAttributedString:thatYouJustMade]

Note: If the textview employs attributes like underlining or fore/background coloring, or italicized/bold fonts, you must get that information from the textStorage and incorporate it into the new attributed string. 注意:如果textview使用诸如下划线或前/背景色或斜体/粗体字体之类的属性,则必须从textStorage获取该信息并将其合并到新的属性字符串中。 It's doable. 这是可行的。 Look at the class refs for NSAttributedString and NSMutableAttributedString. 查看NSAttributedString和NSMutableAttributedString的类引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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