gpt4 book ai didi

ios - 继承 UIButton 并设置 fontSize

转载 作者:行者123 更新时间:2023-11-29 10:48:02 24 4
gpt4 key购买 nike

我正在子类化 UIButton 以设置应用程序的一些默认值,但由于某种原因我无法更改字体大小。

- (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;
}

我可以改变颜色和其他属性,但不能改变字体大小,它没有效果吗?

    idButton *LButton = [idButton buttonWithType:UIButtonTypeRoundedRect];
[LButton setTitle:@"Login" forState:UIControlStateNormal];
LButton.frame = CGRectMake(0, buttonY+(buttonHeight/2), screenWidth, buttonHeight);
[self addSubview:LButton];

最佳答案

您的代码存在问题,UIButton 的方法 setTitle:forState: 会将字体重置为默认值。

一种解决方案是覆盖此方法以使用 NSAttributedString。这也将允许您设置其他属性:

- (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];
}
}

关于ios - 继承 UIButton 并设置 fontSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com