In Windows world you can do this to get text width:
SIZE size;
GetTextExtentPoint(DC, "hello", strlen("hello"), &size)
printf("x = %i y = %i\n", size.x, size.y);
// will print x = 30, y = 15 (for instance)
How can I easily do that on iOS, provided that I have UIView handle on hand?
For iOS (I'll let someone more knowledgeable in Android development answer the second part), you'll want to use a variant of sizeWithFont:
. Depending on whether or not you wish to allow multiple lines you should check out the various methods explained in the documentation I linked.
The one I typically find most useful is sizeWithFont:constainedToSize:
where I set the constrained size to have the limited width that I have available in a view, and the height to CGFLOAT_MAX
, which allows the label to use up as much vertical space as needed (since I often have labels within vertically scrolling views).