如何检查NSTextfield是否已将文本截断(末尾...) [英] How to check if NSTextfield is already truncating the text (... at the end)

查看:140
本文介绍了如何检查NSTextfield是否已将文本截断(末尾...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了如何执行此操作,但是找不到任何答案.
我想知道我的NSTextfield是否已经在不检查其stringValue长度的情况下将文本截断了(...结尾).我需要这样做才能知道是否应该在NSTextfield上设置工具提示(就像一个简单的标签一样).
我不想检查文本字段stringValue的长度的原因是因为有些字符比其他字符占用更多的空间,所以不太准确
谢谢!

I've searched around on how to perform this but I can't find any answer.
I'd like to know if my NSTextfield is already truncating the text (... at the end) without having to check the length of its stringValue. I need to do this to know if I should set a tooltip on my NSTextfield (acting like a simple label).
The reason I don't want to check the length of my textfield's stringValue it's because there are some characters that occupy more space than others, so that's not very accurate
Thanks!

推荐答案

您最好的选择是使用NSTextView而不是NSTextField.如果使用NSTextView,则可以使用textContainer属性获得NSTextViewNSTextContainer.容器可以告诉您containerSize(绘制文本的空间).

Your best bet might be to use an NSTextView instead of a NSTextField. If you use an NSTextView you can get the NSTextContainer of the NSTextView using the textContainer property. The container can tell you the containerSize (the space the text is drawn in).

NSString对象响应方法sizeWithAttributes:.如果使用给定的属性绘制,则可以使用结果NSSize结构获取文本的宽度.请参阅

NSString objects respond to the method sizeWithAttributes:. You can use the resulting NSSize struct to grab the width of the text if drawn with the given attributes. See the "Constants" section of the NSAttributedString Application Kit Additions Reference to figure out what attributes are relevant.

如果containerSize宽度小于sizeWithAttributes:宽度,则文本将被截断.

If the containerSize width is less than the sizeWithAttributes: width then the text will be truncated.

编辑:很抱歉,只有在没有lineFragmentPadding的情况下,这是正确的,但默认的lineFragmentPadding为非零.从containerSize.width减去textContainer.lineFragmentPadding或使用NSTextContainer setLineFragmentPadding:方法将其设置为0.

Apologies, this is only true with no lineFragmentPadding, but the default lineFragmentPadding is non-zero. Either subtract the textContainer.lineFragmentPadding from containerSize.width or use the NSTextContainer setLineFragmentPadding: method to set it to 0.

我想您还可以相对于NSTextField的大小对文本区域进行一些假设,并结合使用sizeWithAttributes: NSString方法,但这并不是那么干净.

I suppose you could also make some assumptions about the text area relative to the size of the NSTextField and use the sizeWithAttributes: NSString method in conjunction with that, but that is not as clean.

我意识到我没有解决OP想要用省略号截断文本的兴趣.下面的示例代码在NSTextView中使用截断.我还认为我不妨抛出一些代码,将NSTextView放在NSBox内,使NSTextView的外观与NSTextField更加相似.使用上面已经提到的信息,对下面的代码进行简单的添加即可添加一个尺寸检查,以确定是否应该显示工具提示.

EDIT 2: I realized I did not address the OP's interest in truncating the text using ellipses. The example code below uses truncation in the NSTextView. I also thought I might as well throw in some code that makes the NSTextView a little more similar in appearance to the NSTextField by putting it inside of a NSBox. Adding a check for size to determine if a tooltip should be displayed would be a simple addition to the code below using the information already mentioned above.

NSString* string = @"Hello World.";

// get the size the text will take up using the default font
NSSize textBounds = [string sizeWithAttributes:@{}];

// Create a border view that looks more or less like the border used around
// text fields
NSBox* borderView = [[NSBox alloc] initWithFrame:NSMakeRect(10, 10, 60, textBounds.height+4)];
[borderView setBoxType:NSBoxCustom];
[borderView setBorderType:NSBezelBorder];
[borderView setContentViewMargins:NSMakeSize(0, 0)];
[borderView setFillColor:[NSColor whiteColor]];

// Create the text view
NSTextView* textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 60, textBounds.height)];
[textView setTextContainerInset:NSMakeSize(2, 0)];
[textView.textContainer setLineFragmentPadding:0];
[textView setEditable:YES];

// Set the default paragraph style so the text is truncated rather than
// wrapped
NSMutableParagraphStyle* parStyle = [[NSMutableParagraphStyle alloc] init];
[parStyle setLineBreakMode:NSLineBreakByTruncatingTail];

// Do not let text get squashed to fit
[parStyle setTighteningFactorForTruncation:0.0];

[textView setDefaultParagraphStyle:parStyle];
[parStyle release];

// Set text
[textView setString:string];

// add NSTextView to border view
[borderView addSubview:textView];
[textView release];

// add NSBox to view you want text view displayed in
[self addSubview:borderView];
[borderView release];

这篇关于如何检查NSTextfield是否已将文本截断(末尾...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆