Rotate NSString... I give up :(

  • Hello,

    I need to draw NSString rotated to 90 degrees. Now I have the code:

    NSRect rcWhereToDraw = NSMakeRect(........); // <- rect is already
    "rotated" here
    [str drawInRect:rcWhereToDraw withAttributes:attributes]; // string
    will be drawn non-rotated

    as far as I know, I need something like this:

    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSRect rcWhereToDraw = NSMakeRect(........);

    float dx = NSMidX(rcWhereToDraw), dy = NSMidY(rcWhereToDraw);

    NSAffineTransform *tr = [NSAffineTransform transform];
    [tr translateXBy:-dx yBy:-dy]; // center or rect will be the center of rotation
    [tr rotateByDegrees:90]; // rotate it
    [tr translateXBy:dx yBy:dy]; // move it back
    [tr concat];

    [str drawInRect:rcWhereToDraw withAttributes:attributes];
    [[NSGraphicsContext currentContext] restoreGraphicsState];

    This doesn't work, where am I wrong?
    Thank you.
  • Shouldn't be this instead (check the translations' signs) ?

    [tr translateXBy:dx yBy:dy]; // center or rect will be the center of
    rotation
    [tr rotateByDegrees:90]; // rotate it
    [tr translateXBy:-dx yBy:-dy]; // move it back

    Le 6 août 08 à 08:46, Vitaly Ovchinnikov a écrit :

    > Hello,
    >
    > I need to draw NSString rotated to 90 degrees. Now I have the code:
    >
    > NSRect rcWhereToDraw = NSMakeRect(........); // <- rect is already
    > "rotated" here
    > [str drawInRect:rcWhereToDraw withAttributes:attributes]; // string
    > will be drawn non-rotated
    >
    > as far as I know, I need something like this:
    >
    > [[NSGraphicsContext currentContext] saveGraphicsState];
    > NSRect rcWhereToDraw = NSMakeRect(........);
    >
    > float dx = NSMidX(rcWhereToDraw), dy = NSMidY(rcWhereToDraw);
    >
    > NSAffineTransform *tr = [NSAffineTransform transform];
    > [tr translateXBy:-dx yBy:-dy]; // center or rect will be the center
    > of rotation
    > [tr rotateByDegrees:90]; // rotate it
    > [tr translateXBy:dx yBy:dy]; // move it back
    > [tr concat];
    >
    > [str drawInRect:rcWhereToDraw withAttributes:attributes];
    > [[NSGraphicsContext currentContext] restoreGraphicsState];
    >
    > This doesn't work, where am I wrong?
    > Thank you.
    >
  • Will test this too, but I don't think it will work.
    In my case I moved rectangle from it's real position to zero, then
    rotate around zero and then moved back.
    In your case I will move it far from zero and then rotate...
    But I will give it a try.

    On Wed, Aug 6, 2008 at 11:20 AM, Jean-Daniel Dupas
    <devlists...> wrote:
    > Shouldn't be this instead (check the translations' signs) ?
    >
    > [tr translateXBy:dx yBy:dy]; // center or rect will be the center of
    > rotation
    > [tr rotateByDegrees:90]; // rotate it
    > [tr translateXBy:-dx yBy:-dy]; // move it back
    >
    >
    > Le 6 août 08 à 08:46, Vitaly Ovchinnikov a écrit :
    >
    >> Hello,
    >>
    >> I need to draw NSString rotated to 90 degrees. Now I have the code:
    >>
    >> NSRect rcWhereToDraw = NSMakeRect(........); // <- rect is already
    >> "rotated" here
    >> [str drawInRect:rcWhereToDraw withAttributes:attributes]; // string
    >> will be drawn non-rotated
    >>
    >> as far as I know, I need something like this:
    >>
    >> [[NSGraphicsContext currentContext] saveGraphicsState];
    >> NSRect rcWhereToDraw = NSMakeRect(........);
    >>
    >> float dx = NSMidX(rcWhereToDraw), dy = NSMidY(rcWhereToDraw);
    >>
    >> NSAffineTransform *tr = [NSAffineTransform transform];
    >> [tr translateXBy:-dx yBy:-dy]; // center or rect will be the center of
    >> rotation
    >> [tr rotateByDegrees:90]; // rotate it
    >> [tr translateXBy:dx yBy:dy]; // move it back
    >> [tr concat];
    >>
    >> [str drawInRect:rcWhereToDraw withAttributes:attributes];
    >> [[NSGraphicsContext currentContext] restoreGraphicsState];
    >>
    >> This doesn't work, where am I wrong?
    >> Thank you.
    >>
    >
    >
  • No, Jean-Daniel's method looks right. The first -translateXBy:yBy:
    moves (0, 0) to the center of your text rectangle. -rotateByDegrees:
    rotates the coordinate system by 90 degrees around that center point,
    then the second call to -translateXBy:yBy: restores the coordinate
    system's (0, 0) point to what it was so that when you draw the text
    in rcWhereToDraw, it will be correctly rotated.

    On Aug 6, 2008, at 1:46 AM, Vitaly Ovchinnikov wrote:

    > Will test this too, but I don't think it will work.
    > In my case I moved rectangle from it's real position to zero, then
    > rotate around zero and then moved back.
    > In your case I will move it far from zero and then rotate...
    > But I will give it a try.
    >
    > On Wed, Aug 6, 2008 at 11:20 AM, Jean-Daniel Dupas
    > <devlists...> wrote:
    >> Shouldn't be this instead (check the translations' signs) ?
    >>
    >> [tr translateXBy:dx yBy:dy]; // center or rect will be the center of
    >> rotation
    >> [tr rotateByDegrees:90]; // rotate it
    >> [tr translateXBy:-dx yBy:-dy]; // move it back
    >>
    >>
    >> Le 6 août 08 à 08:46, Vitaly Ovchinnikov a écrit :
    >>
    >>> Hello,
    >>>
    >>> I need to draw NSString rotated to 90 degrees. Now I have the code:
    >>>
    >>> NSRect rcWhereToDraw = NSMakeRect(........); // <- rect is already
    >>> "rotated" here
    >>> [str drawInRect:rcWhereToDraw withAttributes:attributes]; // string
    >>> will be drawn non-rotated
    >>>
    >>> as far as I know, I need something like this:
    >>>
    >>> [[NSGraphicsContext currentContext] saveGraphicsState];
    >>> NSRect rcWhereToDraw = NSMakeRect(........);
    >>>
    >>> float dx = NSMidX(rcWhereToDraw), dy = NSMidY(rcWhereToDraw);
    >>>
    >>> NSAffineTransform *tr = [NSAffineTransform transform];
    >>> [tr translateXBy:-dx yBy:-dy]; // center or rect will be the
    >>> center of
    >>> rotation
    >>> [tr rotateByDegrees:90]; // rotate it
    >>> [tr translateXBy:dx yBy:dy]; // move it back
    >>> [tr concat];
    >>>
    >>> [str drawInRect:rcWhereToDraw withAttributes:attributes];
    >>> [[NSGraphicsContext currentContext] restoreGraphicsState];
    >>>
    >>> This doesn't work, where am I wrong?
    >>> Thank you.
  • Bear in mind you are moving the co-ordinate system to the object, not
    the object around the co-ordinate system...

    I find it useful to "read" NSAffineTransform operations in reverse
    order. The operations you list in the code are applied to the object
    in the opposite order.

    cheers, Graham

    On 6 Aug 2008, at 6:46 pm, Vitaly Ovchinnikov wrote:

    > Will test this too, but I don't think it will work.
    > In my case I moved rectangle from it's real position to zero, then
    > rotate around zero and then moved back.
    > In your case I will move it far from zero and then rotate...
    > But I will give it a try.
    >
    > On Wed, Aug 6, 2008 at 11:20 AM, Jean-Daniel Dupas
    > <devlists...> wrote:
    >> Shouldn't be this instead (check the translations' signs) ?
    >>
    >> [tr translateXBy:dx yBy:dy]; // center or rect will be the center of
    >> rotation
    >> [tr rotateByDegrees:90]; // rotate it
    >> [tr translateXBy:-dx yBy:-dy]; // move it back
    >
previous month august 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 31
Go to today