• 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: string drawing again
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string drawing again


  • Subject: Re: string drawing again
  • From: Chris Meyer <email@hidden>
  • Date: Thu, 5 Feb 2004 12:08:16 -0800

On Feb 5, 2004, at 11:36 AM, Alastair Houghton wrote:

On 4 Feb 2004, at 23:30, Chris Meyer wrote:

I'm trying to draw a string in a sub-rectangle of a non-flipped graphics context. The string needs to be justified to the sides of the sub-rectangle.

I can't use the NSString methods drawInRect or drawAtPoint because they clip the right side of large italicized strings.

I can't use the text layout classes because they (strangely) require a flipped coordinate system.

Any suggestions?

You can use a subview to draw the text; you only need a single subview, even if you're drawing many strings. A quick breakdown of what you need to do:

1. Create or otherwise obtain a view that can render the text you need. You could use an NSTextView, you could use the window's field editor (which is an NSTextView, although the -fieldEditor:forObject: method returns an NSText, NSTextView's superclass), or you could use your own class that uses NSString or NSAttributedString's methods, or even ATSUI.

2. In your -drawRect:, add the view as a subview of the view you are drawing.

3. For each bit of text:

(i) Move the subview to the appropriate place in your view.

(ii) Configure the subview to render it.

(iii) Send the subview a -display message to make it do the drawing.

4. Remove the subview again.

This type of approach has the advantage that your subview can be flipped even though the view it is in is not. When I started using Cocoa, I kept thinking of the views like windows in Win32 (so moving them around or altering the hierarchy would cause a redraw), whereas they are actually much lighter-weight than that... so it is perfectly reasonable to add and remove them like this.

Thanks for this tip. I am definitely thinking of 'heavier weight' subviews like Win32 so I will give the sub-views a shot.

Just as you sent this message I was finishing up with code to use a affine transform to flip the view; then flipping the font matrix by hand.

See http://cocoa.mamasam.com/MACOSXDEV/2001/11/2/18891.php for most of the gory details. Thanks Andrew Lindesay. The answer to his question in his message, by the way, is that he needs to set the text container size. I didn't bother replying to the message since it is over two years old.

My current code works fine but I'm a little nervous about the layout resulting from the algorithm remaining consistent in future releases of Cocoa. I guess that holds for the sub-view approach also.

I did notice that [NSAttributedString drawInRect and drawAtPoint] use the 'original' typography behavior in Panther, in case anyone cares. Those two methods also seem to have some dependency on the enclosing NSView that shows up as a bug in layout.


BTW, it isn't strange that the text system requires a flipped co-ordinate system. If the co-ordinates weren't flipped, then the text system wouldn't be able to render the first line of text until it had completed layout for *all* of the text in the view, because there would be no way to know how tall the text was.

This seems to be true only if the top of the drawing rectangle is not known. I can't think of a case where it wouldn't be known...

My algorithm works fine and is relatively high performance (the only thing that slows it down is flipping the font matrix).


Kind regards,

Alastair.
_______________________________________________
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: 
 >string drawing again (From: Chris Meyer <email@hidden>)
 >Re: string drawing again (From: Alastair Houghton <email@hidden>)

  • Prev by Date: Re: Assign icon to a file
  • Next by Date: Re: Background app, NSStatusItem, tight loop, event handling
  • Previous by thread: Re: string drawing again
  • Next by thread: Re: string drawing again
  • Index(es):
    • Date
    • Thread