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

mklgxw1f  于 2022-11-26  发布在  iOS
关注(0)|答案(4)|浏览(240)

我试图改变按钮本身的字体大小后,它被按下。

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似乎适用,但与字体大小我面临的问题。

zsohkypk

zsohkypk1#

首先,这里是当你点击UIButton时的事件序列。
1.按钮会将自己的isHighlighted属性设定为true
1.该按钮触发任何Touch Down操作,如果您拖动手指,可能会触发多次。
1.该按钮触发任何Primary Action Triggered操作(如您的buttonTapped)。
1.该按钮触发任何Touch Up操作。
1.按钮会将自己的isHighlighted属性设定为false

  • 每次isHighlighted更改时,按钮都会将其样式更新为它认为应该的外观。* 因此,在buttonTapped之后的片刻,您按下的按钮会将您选择的字体覆盖为它自己的字体。
    • 不要在生产环境中使用它**一旦你开始覆盖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
}

我认为有一种更简单的方法可以做到这一点。无论你用哪种方法工作,确保它在按钮发生其他变化时也能工作,比如在按钮上发生其他事件将isEnabled设置为false

sqougxex

sqougxex2#

你可能想要这样的东西

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))
    }
  }
}
vc9ivgsu

vc9ivgsu3#

class ViewController: UIViewController {
     @IBOutlet weak var buttonToResize: UIButton!
  
    @IBAction func buttonTapped(_ sender: UIButton) {
    sender.titleLabel?.font = .systemFont(ofSize: 30)
    }
  }

这应该可以解决问题。请使用sender标记代替IBOutlet。

laawzig2

laawzig24#

Cowirrie分析让我想到了这个解决方案(经过测试)

@IBAction func testX(_ sender: UIButton) {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1)  { 
        sender.titleLabel?.font = sender.titleLabel?.font.withSize(32) 
    }
}

相关问题