views:

448

answers:

3

How to know how much pixels my text is taking in a UILabel ?
I want to insert a small picture in between text.
Can be usefull to adjust my label width.

+5  A: 

There are a few methods in NSString added by UIKit that can give you the dimensions of the string if it were to be drawn. The full reference is here. The methods you care about are probably these:

  • sizeWithFont:
  • sizeWithFont:constrainedToSize:
  • sizeWithFont:constrainedToSize:lineBreakMode:
  • sizeWithFont:forWidth:lineBreakMode:
  • sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
Kevlar
+2  A: 

-[NSString sizeWithFont:] is part of NSString(UIStringDrawing).

Graham Lee
+2  A: 

take a look at this post http://stackoverflow.com/questions/1340667/pixel-width-of-the-text-in-a-uilabel

Steve918