Reputation: 20021
Trying to draw the text using CGContextShowText.How to make the text centered in the specific rect?
Note: drawInRect can do this job.I am asking particularly about CGContextShowText
Upvotes: 2
Views: 360
Reputation: 53561
If you really want to use CGContextShowText
(Core Text would be better), you'll have to do the math yourself, i.e. figure out how big the text is, and subtract half of that from the x coordinate of your center point.
To determine the size of the text, you could set the text drawing mode to kCGTextInvisible
(using CGContextSetTextDrawingMode
), draw the text once, check how far the text position has moved (using CGContextGetTextPosition
before and after drawing), and then draw the text again with a visible drawing mode at the correct location.
Upvotes: 1