热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我可以在iOS8中动态更改UIButton的字体吗?-CanIchangethefontofaUIButtondynamicallyiniOS8?

IhavetwobuttonsonmyViewController.LetscallthemA&B.我的ViewController上有两个按钮。我们称他们为A

I have two buttons on my View Controller. Let's call them A & B.

我的View Controller上有两个按钮。我们称他们为A&B。

After A is tapped, I would like Button A's font to be bolded, and B's font to be unbolded.

在点击A之后,我希望Button A的字体用粗体显示,而B的字体要用unbolded。

Vice Versa for tapping Button B.

用于点击按钮B的副Versa。

Is this possible?

这可能吗?

1 个解决方案

#1


Yes! You just need to have a reference to both buttons and when you click on a button be sure it has a target attached to it.

是!您只需要引用两个按钮,当您单击按钮时,请确保它附有目标。

You can get a reference to your buttons either through an IBOutlet from a xib/ storyboard or add the buttons as properties to your viewcontroller and initialize/add them to your view early in your viewcontroller's lifecycle

您可以通过xib / storyboard中的IBOutlet获取对按钮的引用,或者将按钮作为属性添加到viewcontroller,并在viewcontroller的生命周期的早期初始化/添加到视图中

You can have a 'target' attached to a button either by dragging an action from your xib/storyboard to your Viewcontroller or by adding it to the button once it has been initialized ie:

您可以通过将动作从xib / storyboard拖动到Viewcontroller,或者在初始化按钮后将其添加到按钮,即可将“目标”附加到按钮上,即:

//adding a target to first button
[self.button1 addTarget:self action:@selector(didTapOnButton1:) forcontrolEvents:UIControlEventTouchUpInside];
//...

Sample methods your buttons can call once they are touched up:

触摸后按钮可以调用的示例方法:

 -(void)didTapOnButton1:(id)sender{
  self.button2.titleLabel.fOnt= [UIFont systemFontOfSize:14 weight:4];
  self.button1.titleLabel.fOnt= [UIFont systemFontOfSize:14 weight:10];
 }
 -(void)didTapOnButton2:(id)sender{
 self.button1.titleLabel.fOnt= [UIFont systemFontOfSize:14 weight:4];
 self.button2.titleLabel.fOnt= [UIFont systemFontOfSize:14 weight:10];
}

推荐阅读
author-avatar
宝宝田小丫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有