I need to draw text in the middle of a view, the text can be 1 letter or few words.
I am drawing the text in the - (void)drawRect:(CGRect)rect
method.
What I want is that the text will be centered horizontally and vertically (I know how to do that). BUT I want the font size update according to the text length so:
a string like "S" will be drawn in a 90 points font size but "hello world" will be drawn in 30 points font size.
Any Ideas?
UILabel can be used to center vertically and horizontally. Using adjustsFontSizeToFitWidth
it can also shrink the text to fit within the label's width.
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 10.0; // or whatever...
If you need more control of the size I think you have to do the positioning your self. You can use the sizeWithFont:
and sizeWithFont:forWidth:lineBreakMode:
messages on a NSString to get the size of your string with a certain font - and then figure out what size to use and where to draw it. See the documentation.