Figure out the size of an NSAttributedString

  • I know this might seem like a weird idea, but I need to figure out the
    size (width and height) of an NSAttributedString that I am drawing...
    basically I need to know it because I am drawing it inside a larger
    image and I need to position it... however the string can be of any
    font/size/length... is there a way to do this?? I'm guessing the
    height would probably be the font's point? (i.e. font size 24 would
    result in a height of 24pt?)  how about the width? any way to find
    that out before drawing it? (or even after drawing it to an
    intermediate image if I really need to?)

    Any ideas are welcomed!

    Jean-Nicolas Jolivet
    <silvertab...>
    http://www.silverscripting.com
  • On Nov 26, 2008, at 3:14 PM, Jean-Nicolas Jolivet wrote:

    > I know this might seem like a weird idea, but I need to figure out
    > the size (width and height) of an NSAttributedString that I am
    > drawing... basically I need to know it because I am drawing it
    > inside a larger image and I need to position it... however the
    > string can be of any font/size/length... is there a way to do this??
    > I'm guessing the height would probably be the font's point? (i.e.
    > font size 24 would result in a height of 24pt?)  how about the
    > width? any way to find that out before drawing it? (or even after
    > drawing it to an intermediate image if I really need to?)

    You'll find the methods you need right alongside those for drawing the
    attributed string, in NSStringDrawing.h.

    Douglas Davidson
  • On Nov 26, 2008, at 4:14 PM, Jean-Nicolas Jolivet wrote:

    > I know this might seem like a weird idea, but I need to figure out
    > the size (width and height) of an NSAttributedString that I am
    > drawing... basically I need to know it because I am drawing it
    > inside a larger image and I need to position it... however the
    > string can be of any font/size/length... is there a way to do this??

    Yes: <http://developer.apple.com/documentation/Cocoa/Conceptual/TextLayout/Tasks/
    StringHeight.html#//apple_ref/doc/uid/20001809
    >

    In addition to what it says in the guide, you also need to set the
    typesetter behavior to NSTypesetterBehavior_10_2_WithCompatibility or
    you will get inaccurate results if this is a string that is drawn
    using one of the built-in methods for drawing.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • Le 27 nov. 08 à 00:14, Jean-Nicolas Jolivet a écrit :

    > I know this might seem like a weird idea, but I need to figure out
    > the size (width and height) of an NSAttributedString that I am
    > drawing... basically I need to know it because I am drawing it
    > inside a larger image and I need to position it... however the
    > string can be of any font/size/length... is there a way to do this??
    > I'm guessing the height would probably be the font's point? (i.e.
    > font size 24 would result in a height of 24pt?)  how about the
    > width? any way to find that out before drawing it? (or even after
    > drawing it to an intermediate image if I really need to?)
    >
    > Any ideas are welcomed!
    >

    Google:  "getting size of nsattributedstring"

    At least the first ten results.

    And if you don't want to search, this is - [NSAttributedString size].

    http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSAttributedString_AppKitAdditions/Reference/Reference.html#/

    /apple_ref/occ/instm/NSAttributedString/size
  • On Nov 26, 2008, at 5:14 PM, Jean-Nicolas Jolivet wrote:

    > I know this might seem like a weird idea, but I need to figure out
    > the size (width and height) of an NSAttributedString that I am
    > drawing... basically I need to know it because I am drawing it
    > inside a larger image and I need to position it... however the
    > string can be of any font/size/length... is there a way to do this??
    > I'm guessing the height would probably be the font's point? (i.e.
    > font size 24 would result in a height of 24pt?)  how about the
    > width? any way to find that out before drawing it? (or even after
    > drawing it to an intermediate image if I really need to?)
    >
    From "NSAttributedString Application Kit Additions Reference":

    size
    Returns the bounding box of the marks that the receiver draws.

    - (NSSize)size

    Works for me, at least in recent OS incarnations.

    Gerd
  • Thanks to all....

    Somehow I assumed it wouldn't be something that is commonly used...
    now that I think about it, it makes perfect sense that one would need
    to know the size of a string that is do be drawn!...  apparently I
    should've dig a little deeper! A hearty RTFM to myself! :)

    Thanks again...
    J-N

    On 26-Nov-08, at 6:14 PM, Jean-Nicolas Jolivet wrote:

    > I know this might seem like a weird idea, but I need to figure out
    > the size (width and height) of an NSAttributedString that I am
    > drawing... basically I need to know it because I am drawing it
    > inside a larger image and I need to position it... however the
    > string can be of any font/size/length... is there a way to do this??
    > I'm guessing the height would probably be the font's point? (i.e.
    > font size 24 would result in a height of 24pt?)  how about the
    > width? any way to find that out before drawing it? (or even after
    > drawing it to an intermediate image if I really need to?)
    >
    > Any ideas are welcomed!
    >
    > Jean-Nicolas Jolivet
    > <silvertab...>
    > http://www.silverscripting.com

    Jean-Nicolas Jolivet
    <silvertab...>
    http://www.silverscripting.com
previous month november 2008 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today