如何在外面触摸时更改按钮字体?

时间:2016-10-18 19:46:32

标签: ios swift uibutton

因此,我尝试在按下按钮时更改按钮的字体,然后在用户触摸另一个按钮时恢复字体。当用户触摸内部时更改字体当前有效,但是我从故事板创建了一个用于touchUpOutside操作的插座,并尝试回收代码但它无法正常工作。

@IBAction func touchUpInside(_ sender: AnyObject) {
    sender.titleLabel?!.font = UIFont(name: "Futura-CondensedExtraBold", size: 25)
}
@IBAction func touchUpOutside(_ sender: AnyObject) {
    sender.titleLabel?.font = UIFont(name: "Futura-Medium", size: 22)
}

2 个答案:

答案 0 :(得分:0)

你是说你有一个按钮吗?当你触摸时,文字字体会改变,当你在同一个按钮上触摸按钮时,文字字体将变为另一种恢复字体。

如果这是你要找的答案,除了答案之外别忘了。

// The Button As An Outlet
@IBOutlet weak var theButton: UIButton!

// The Button using Touch Up Inside
@IBAction func theButtonInside(_ sender: AnyObject) {
     sender.titleLabel?!.font = UIFont(name: "Futura-CondensedExtraBold", size: 25)
}

// The Button using Touch Up Outside
@IBAction func theButtonOutside(_ sender: AnyObject) {
    //This is a reference to the button as an outlet.
    theButton.titleLabel?.font = UIFont(name: "Futura-Medium", size: 22)
}

答案 1 :(得分:0)

按button2

时,恢复button1的字体

如果这是您正在寻找的答案,请不要忘记除了答案。

//Button 1 as outlet.
@IBOutlet weak var button1Outlet: UIButton!

//Button 1 as touchUpInside.
@IBAction func button1(_ sender: AnyObject) {
    sender.titleLabel?!.font = UIFont(name: "Futura-CondensedExtraBold", size: 25)
}

//Button 2 as touchUpInside.
@IBAction func button2(_ sender: AnyObject) {

//Referenceing button 1 outlet.
    button1Outlet.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
}