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!
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
I think you can do it by looking at the frame of the text field's field editor. You check its width in controlTextDidBeginEditing, and then again in controlTextDidEndEditing. If the latter value is larger, then the text has been truncated. The following is implemented in the text field's delegate (I created an ivar for initialWidth):
This seems to work most of the time, including if you type in a long string, then delete until it fits again. In a few cases, it gave me the log message when I was one character past the end of the text field, but I did not get the ellipsis when editing ended.
Swift 4 solution using ipmcc as base
It will resize one by one until it fit or fontsize = 3
Your best bet might be to use an
NSTextView
instead of aNSTextField
. If you use anNSTextView
you can get theNSTextContainer
of theNSTextView
using thetextContainer
property. The container can tell you thecontainerSize
(the space the text is drawn in).NSString
objects respond to the methodsizeWithAttributes:
. You can use the resultingNSSize
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.If the
containerSize
width is less than thesizeWithAttributes:
width then the text will be truncated.EDIT: Apologies, this is only true with no
lineFragmentPadding
, but the defaultlineFragmentPadding
is non-zero. Either subtract thetextContainer.lineFragmentPadding
fromcontainerSize.width
or use theNSTextContainer
setLineFragmentPadding:
method to set it to0
.I suppose you could also make some assumptions about the text area relative to the size of the
NSTextField
and use thesizeWithAttributes:
NSString
method in conjunction with that, but that is not as clean.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 theNSTextView
a little more similar in appearance to theNSTextField
by putting it inside of aNSBox
. 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.For future visitors, you can use the
-[NSCell expansionFrameWithFrame:inView:]
method to determine if truncation is taking place. From the header:In short, you can tell if an
NSTextField
is truncating like this: