繁体   English   中英

按下时如何为 UIButton 设置不同的字体大小

[英]How to set different font size for UIButton when it is pressed

我正在尝试在按下按钮后更改按钮本身的字体大小。

class ViewController: UIViewController {
   @IBOutlet weak var buttonToResize: UIButton!

   @IBAction func buttonTapped(_ sender: UIButton) {
        buttonToResize.titleLabel!.font = UIFont(name: "Helvetica", size: 40)
// Also tried buttonToResize.titleLabel?.font = UIFont .systemFont(ofSize: 4)
}

但是,未应用更改。

对我来说有趣的是,如果我在按下初始按钮(第一个按钮)后尝试调整其他按钮(第二个按钮)的大小,它会按预期工作。

像这样:

class ViewController: UIViewController {
@IBOutlet weak var buttonToResize: UIButton!
@IBOutlet weak var secondButtonToResize: UIButton!

@IBAction func buttonTapped(_ sender: UIButton) {
    secondButtonToResize.titleLabel!.font = UIFont(name: "Helvetica", size: 40)
}

其他属性,如 backgroundColor 似乎适用,但是字体大小我面临问题。

你可能想要这样的东西

struct MyView: View {
  @State var pressed: Bool = false

  var body: some View {
    Button(action: {
      withAnimation { 
        pressed = true 
      }
    }) {
      Text("Hello")
    }
    Button(action: {
    }) {
      Text("Hello")
         .font(pressed ? .system(size: 40) : .system(size: 20))
    }
  }
}

首先,这是您点击UIButton时的事件序列。

  1. 该按钮将其自己的isHighlighted属性设置为true
  2. 如果您拖动手指,该按钮会触发任何Touch Down操作,可能会触发多次。
  3. 该按钮触发任何Primary Action Triggered的操作(例如您的buttonTapped )。
  4. 该按钮会触发任何Touch Up操作。
  5. 该按钮将其自己的isHighlighted属性设置为false

每次 isHighlighted 更改时,按钮都会将其样式更新为它认为的外观。 所以在buttonTapped之后,你按下的按钮会用它自己的字体覆盖你选择的字体。

通过创建UIButton子类来确保您理解它是值得探索的。 不要在生产中使用它 一旦开始覆盖 UIButton 的部分内容,您需要覆盖所有部分。

// This class is to demonstrate the sequence of events when you press a UIButton.
// Do not use in production.
// To make this work properly, you would also have to override all the other properties that effect UIButton.state, and also UIButton.state itself.
class MyOverrideHighlightedButton : UIButton {
    // Define a local property to store our highlighted state.
    var myHighlighted : Bool = false
    
    override var isHighlighted: Bool {
        get {
            // Just return the existing property.
            return myHighlighted
        }
        
        set {
            print("Setting MyOverrideHighlightedButton.isHighlighted from \(myHighlighted) to \(newValue)")
            
            myHighlighted = newValue
            
            // Since the UIButton remains unaware of its highlighted state changing, we need to change its appearance here.
            // Use garish colors so we can be sure it works during testing.
            if (myHighlighted) {
                titleLabel!.textColor = UIColor.red
            } else {
                titleLabel!.textColor = titleColor(for: .normal)
            }
        }
    }
}

那么它从哪里不断地拉出它的旧字体呢? 在加载视图时,它将应用UIAppearance设置,但是当您按下按钮时,这些设置也会被丢弃。 iOS 15+,看起来它使用了新的UIButton.Configuration结构。 所以你可以把它放在你的buttonTapped中:

// The Configuration struct used here was only defined in iOS 15 and
// will fail in earlier versions.
// See https://developer.apple.com/documentation/uikit/uibutton/configuration
sender.configuration!.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
    var outgoing = incoming
    // We only want to change the font, but we could change other properties here too.
    outgoing.font = UIFont(name: "Zapfino", size: 20)
    return outgoing
}

我想认为有一种更简单的方法可以做到这一点。 无论您采用哪种工作方式,请确保它在您的按钮发生其他更改时也能正常工作,例如其他一些事件设置isEnabledfalse

暂无
暂无

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

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