• 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: Best way to obtain the width of a piece of text?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to obtain the width of a piece of text?


  • Subject: Re: Best way to obtain the width of a piece of text?
  • From: Steve Sims <email@hidden>
  • Date: Thu, 8 Apr 2004 20:16:57 -0400

On 8 Apr 2004, at 19:03, Douglas Davidson wrote:
I think what you really want is -[NSString sizeWithAttributes:].

Doh! Yep, that makes finding out the width of a string much easier.

However there was a reason why I was using the text layout system - I was making use of other parts of NSLayoutManager in my program. The sample code I gave was just a small snippet.

I will try using [NSString sizeWithAttributes:] later, once I've worked out how to create a suitable attributes dictionary. I've never used NSDictionary objects before so it'll take me a little while.

Meanwhile when I was in the process of looking up the documentation for [NSString sizeWithAttributes:] I read up a little on NSAttributedString too. I am guessing that using [NSLayoutManager attributedString] combined with [NSAttributedString size] should give me the same results as using [NSString sizeWithAttributes:]. I therefore tried this method first.

Unfortunately this also fails to give what I would consider to be correct results. Using the long example string I gave in my last message returns a width of 1008.789, which matches the width of the string up to the tab character. The height returned however (which I am ignoring) is double, implying that the string with the tab has been rendered onto two lines. Increasing the length of the string further and adding an additional tab results in a height of three times the original size, implying the bounding box of the string is being calculated based on rendering the string onto three lines.

I would expect that using [NSString sizeWithAttributes:] will return the same results as the [NSAttributedString size] version I am using right now.

The implication of the results I am seeing is that after a certain point a tab character will be interpreted as a new line, rather than as a tab. This is not exactly what I would expect.

So the real question is how do I work out the width of a string that is reasonably long and contains at least one tab character as it would appear if rendered on a single line?

Steve
_______________________________________________
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.


  • Follow-Ups:
    • Re: Best way to obtain the width of a piece of text?
      • From: Douglas Davidson <email@hidden>
References: 
 >Best way to obtain the width of a piece of text? (From: Steve Sims <email@hidden>)
 >Re: Best way to obtain the width of a piece of text? (From: Douglas Davidson <email@hidden>)

  • Prev by Date: Custom View Tracking Rectangle
  • Next by Date: I need more mouseDragged events
  • Previous by thread: Re: Best way to obtain the width of a piece of text?
  • Next by thread: Re: Best way to obtain the width of a piece of text?
  • Index(es):
    • Date
    • Thread