I want to change the default font for all UITextViews. It seems that the easiest way to do this is via custom category. I found this solution: Change the default systemFont used by controls and tried to implement it.
But my UITextViews are added programmatically so the awakeFromNib function is not called. I tried to move it to initWithFrame like this:
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
float size = [self.font pointSize];
NSString *stringfontstyle=self.font.fontName;
if([stringfontstyle rangeOfString:@"Bold"].location != NSNotFound) {
self.font = [UIFont fontWithName:@"Avenir-Black" size:size];
}
else if ([stringfontstyle rangeOfString:@"Italic"].location != NSNotFound) {
self.font = [UIFont fontWithName:@"Avenir-Oblique" size:size];
}
else if ([stringfontstyle rangeOfString:@"Medium"].location != NSNotFound) {
self.font = [UIFont fontWithName:@"Avenir-Medium" size:size];
}
else {
self.font = [UIFont fontWithName:@"Avenir-Roman" size:size];
}
}
return result;
}
Weird is that if my category contains initWithFrame function, the UITextView disappears. What is it that I'm missing?
Note: I'm using autoLayout so the initWithFrame is called with CGRectZero, but I suppose that isn't the problem.
EDIT:
The problem is that the font is null when the UITextView is initiated. So what method would be appropriate to place the code into?