Reputation: 43
I need to fill in a rect with some text. The size of rect is fixed.
I use the following function:
- (void)drawText:(NSString *)text onView:(UIView *)view inRect:(CGRect)rect
{
UILabel *lbl = [[UILabel alloc] initWithFrame:rect];
lbl.minimumFontSize = 1;
lbl.font = [UIFont systemFontOfSize:30];
[lbl setText:text];
lbl.adjustsFontSizeToFitWidth = YES;
[view addSubview:lbl];
[lbl release];
}
Then I write something similar to this:
[self drawText:@"Asdfghjkl;" onView:view inRect:CGRectMake(45, 40, 85, 13)];
[self drawText:@"qwertyui" onView:view inRect:CGRectMake(173, 31, 126, 22)];
And I get the cutted text. Where is my mistake?
Upvotes: 0
Views: 2364
Reputation: 340
As Gargolev said, it will adjust to fit width only.
You should hence choose the right fontSize to fit the height you're provided with.
To do that at runtime without prior knowledge of the font you're going to use, a simplistic solution would be:
UILabel *label = [[UILabel alloc] initWithFrame:myView.bounds];
label.font = [UIFont fontWithName:@"Helvetica" size:1.f];
float fontAspectFactor = 1.f/label.font.lineHeight;
label.font = [UIFont fontWithName:@"Helvetica" size:myView.height*fontAspectFactor];
[myView addSubview:label];
Upvotes: 2
Reputation: 43
I think the main trouble is that some text fits width only but it must fit both width and height. So the fastest solution for me is to make a bigger transparent UILabel and put a text inside it using alignment
Upvotes: 0