UILabel centered text – getting the text’s rect

UILabel centered text – getting the text’s rect


I recently had a section of user interface where I had a UILabel that took up an iPhone’s full width. It contained text which would be dynamic over time, meaning that the text would update at times. I wanted to place an image icon beside the first character of the label, but I needed to know where to place it. I had done it in the past but honestly forgot exactly how to do it.

Kyle Sluder from the Apple Cocoa-Dev list reminded me what I should be looking for.


Doh! I should have decided to RTFM — read the documentation. You can get a rect for the label, but you can’t use all of it for positioning. Here is sample code in how to get the job done.

Pretty easy. I just forgot how to do it. This is for the whole text, not substrings within it.


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.