• 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
Calculating Lines of Text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calculating Lines of Text


  • Subject: Calculating Lines of Text
  • From: Mike Wright <email@hidden>
  • Date: Sat, 7 Oct 2006 15:18:12 -0500

How can I get at the bounding rects for wrapped text in each cell of an NSTableColumn? The following explains why I want to know, and what I've tried so far.

I have an NSTable view for which I'm calculating - tableView:heightOfRow:. I'm causing this calculation to take place whenever -controlTextDidChange is called (by calling -reloadData:), so that I get live resizing of rows as text is typed into them. (Live resizing is a preference that can be turned off by the user, with resizing triggered by a hot key.) And, of course, the calculation is also carried out whenever -reloadData: is called for other reasons.

There is only one column involved in the calculation, and I know the cell content for each row and the height requirement for a single line of text, based on the font. (It's monostyled text only.)

What I need to calculate for each cell is either (1) how many lines of text will appear or (2) the height of the rect required to display the wrapped text (the width of the frame being the -width: of the column).

I first tried (1), using NSFont's (now deprecated) -widthOfString:, dividing it by the column width to get a line count. This doesn't produce an accurate line count, because some lines end up longer than expected, due to the way wrapping works--some extra blank space can occur at the end of lines when a word gets bumped down to the next line. The result is that I can end up with a row that's one line too short (vertically).

Next I tried (2). Not knowing how to get the frame that is actually used by the cells themselves for drawing text, I've experimented with shoving the text into an NSTextView with the appropriate font set, and with a width that matches the column width, and then using NSLayoutManager's -boundingRectForGlyphRange:inTextContainer: to get a bounding rect. The first result is a rect with a height that, for some reason, is not an even multiple of the text line height as drawn in the table cell. I've tried regularizing that on the basis of the number of lines calculated in method (1), but sometimes end up with an extra empty line. That may just be the result of programming too late without coffee, and I'm sure I can figure out where I've gone wrong.

However, the big problem with using a separate text view for these calculations is that it slows down typing by quite a bit--especially on non-Intel machines. If I could just look at each cell of the column, that should be much faster, but I haven't found how that can be done. I've optimized method (2) by keeping NSTextView and NSFont variables on hand, but the process is still complex and extremely slow. Even resizing the window or the enclosing view can get very jerky--regardless of whether live resizing is turned on or not.

Any suggestions? As I said at the beginning, what I would really like is some way to get at the actual text bounding rects for all the cells in the column, but if that's not possible, something more efficient than my method (2) would be nice. I can provide some code to critique if it comes to that.

Thanks,
Mike Wright
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: How to check if an image has an alpha channel?
  • Next by Date: Re: FSAllocateFork
  • Previous by thread: Re: MySQL cocoa help!
  • Next by thread: Dynamic Instance Variables, is id the answer?
  • Index(es):
    • Date
    • Thread