2

If this is a repeat, let me know and I'll remove it immediately--I've had the devil of a time searching for the answer, since "\t" returns searches without the forward slash.

I need to find the width of an NSString that contains begins with \t\t . Using sizeWithFont doesn't do it, since it just ignores the space. Is there a way to measure how much space the tab takes up?

Joel Derfner
  • 2,207
  • 6
  • 33
  • 45
  • Have your tried with NSAttributedString? http://stackoverflow.com/questions/3429671/how-to-get-the-width-of-an-nsstring – Master Stroke Jan 23 '13 at 02:52
  • No. I haven't used NSAttributedString before, but an initial glance at the documentation doesn't make it immediately clear to me how I could use it to accomplish this. Do you have any specific thoughts about what I could do with NSAttributedString? – Joel Derfner Jan 23 '13 at 03:05

1 Answers1

3

Not sure what your code was but this seemed to work for me.

NSString *delMessage = @"\t";
CGSize theSize = [delMessage sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:30]];
NSLog(@"length %f",theSize.width);

log was length 8.000000

NSString *delMessage = @"\t\t";
CGSize theSize = [delMessage sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:30]];
NSLog(@"length %f",theSize.width);

log was length 16.000000

iDev
  • 23,310
  • 7
  • 60
  • 85
arcady bob
  • 1,536
  • 1
  • 10
  • 5
  • Well, that was easy. Whoops. I think my problem was that I'd been measuring a `UITextView`'s `sizeWithFont` and, when it didn't take the `\t` into account, assumed that that applied to `NSString` as well. Just goes to show you--never make even the most basic assumptions! – Joel Derfner Jan 23 '13 at 03:11