i am subclassing UIButton to set some defaults for the app, but i for some reason i am not able to change the font size.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// id button, set defaults
[self setTintColor:[UIColor blueColor]];
[self.titleLabel setFont:[UIFont systemFontOfSize:40.0]];
[self.layer setBorderWidth:1.0f];
[self.layer setBorderColor:[[UIColor blackColor] CGColor]];
}
return self;
}
I can change the color and the other propertise, but not the fontsize, it has no effect?
idButton *LButton = [idButton buttonWithType:UIButtonTypeRoundedRect];
[LButton setTitle:@"Login" forState:UIControlStateNormal];
LButton.frame = CGRectMake(0, buttonY+(buttonHeight/2), screenWidth, buttonHeight);
[self addSubview:LButton];
The problem with your code is that UIButton's method setTitle:forState: will reset the font back to the default.
One solution would be to override this method to use the NSAttributedString. This will also allow you to set other attributes:
- (void) setTitle:(NSString *)customTitle forState:(UIControlState)state {
if (customTitle) {
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:customTitle];
NSRange range = NSMakeRange(0, [customTitle length]);
[mas addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:40.0]
range:range];
[mas addAttribute:NSKernAttributeName
value:@(1.1)
range:range];
[self setAttributedTitle:mas forState:state];
} else {
[super setTitle:customTitle forState:state];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With