简体   繁体   English

按下时ios更改按钮标题字体

[英]ios change button title font when pressed

i'm new on ios and i wonder if is it possible to change button's title font when it is pressed, i can change button's color when its highlighted as: 我是ios的新手,我想知道是否可以在按下按钮时更改按钮的标题字体,当按钮高亮显示为时,我可以更改按钮的颜色:

[button setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateHighlighted];

is there anything like this to change button's font size, without writing button pressed methods.(i want to change font on highlighted state, so i'm not sure if buttonPressed method will work clearly) 是否有这样的东西来改变按钮的字体大小,而无需编写按钮按下的方法。(我想在突出显示的状态下更改字体,所以我不确定buttonPressed方法是否能正常工作)

You can use the titleLabel property: 您可以使用titleLabel属性:

button.titleLabel.font = [UIFont systemFontOfSize: 12];

EDIT : You'll have to use it in the actions UIControlEventTouchDown , UIControlEventTouchUpInside and UIControlEventTouchUpOutside . 编辑 :您必须在动作UIControlEventTouchDownUIControlEventTouchUpInsideUIControlEventTouchUpOutside使用它。

If you are insistent upon not writing a button pressed method, you could try your luck with attributed strings. 如果您坚持不编写按按钮的方法,则可以尝试使用属性字符串来运气。 UIButton has the method UIButton有方法

– setAttributedTitle:forState:

You can change the highlighted state to use a version of your string with the correct font that way. 您可以更改突出显示的状态,以使用正确字体的字符串版本。

You can learn more about attributed strings in the Apple Documentation . 您可以在Apple文档中了解有关属性字符串的更多信息。

Yet another solution is to use custom images for your buttons. 另一个解决方案是对按钮使用自定义图像。 And use the method 并使用方法

– setImage:forState:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setTitle:@"Click Button" forState:UIControlStateNormal];
myButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[myButton.titleLabel setFont:[UIFont systemFontOfSize:10]];
[self.view addSubview:myButton];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM