• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: how to determine pixel width of a string in a text field
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to determine pixel width of a string in a text field


  • Subject: Re: how to determine pixel width of a string in a text field
  • From: Henry McGilton <email@hidden>
  • Date: Sat, 15 Feb 2003 09:10:40 -0800

On Friday, February 14, 2003, at 09:04 PM, Donald Hall wrote:

Occasionally some of the path names I want to appear in a text field are too long for the field. I had hoped that right justifying the field would cause the (more interesting) end parts of the path to appear in the field and cut off the names of directories higher in the path on the left. Unfortunately, this does not seem to be the case. If the string is too long to fit in a text field, it is always the right end that gets cut off, regardless of justification. The only thing I could see to do would be to calculate the pixel width of the string and display only that part of the string that would fit into the text field.

It looks like I should be using the AppKit NSStringAdditions methods sizeWithAttributes: and drawInRect:withAttributes: However, I'm not sure what to do about defining the attributes. Presumably the attributes dictionary will need at least one entry, for the key NSFontAtrributeName. I am only using the default system font. How do I specify this? Is there a constant defined somewhere, or must I explicitly create an NSFont object for Helvetica 12 point (assuming that is the right one)?

See the systemFontOfSize method description in NSFont


You get your system font like this:

your_system_font = [NSFont systemFontOfSize: whatever_size]

Then you set up your font dictionary:

fontdictionary = [[NSMutableDictionary dictionaryWithCapacity: number_of_slots]
[fontdictionary setObject: your_system_font forKey: NSFontAttributeName];
[fontdictionary setObject: your_text_color forKey: NSForegroundColorAttributeName];

I don't know if you actually need to set the foreground color attribute, but
I do that for completeness.

You determine the width of your string like this:

width_of_your_string = [the_string sizeWithAttributes: fontdictionary];

And I am drawing the captions using drawAtPoint:

[the_string drawAtPoint: the_point withAttributes: fontdictionary];


===============================+============================
Henry McGilton | Trilithon Software
Boulevardier, Java Composer | Seroia Research
-------------------------------+----------------------------
mailto:email@hidden | http://www.trilithon.com
===============================+============================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >how to determine pixel width of a string in a text field (From: Donald Hall <email@hidden>)

  • Prev by Date: Re: Get path to user preference folder, updated
  • Next by Date: ProjectBuilder
  • Previous by thread: how to determine pixel width of a string in a text field
  • Next by thread: AddressBook problems
  • Index(es):
    • Date
    • Thread