簡體   English   中英

如何使 SwiftUi 按鈕的文本變大和/或加粗

[英]How do I make a SwiftUi Button's text larger and/or bold

假設我在 SwiftUI 中有一個按鈕:

Button("Tap me") {
    print("Button tapped!")
}

我怎樣才能使按鈕文本的字體大小更大更粗?

Button的標簽上使用bold() Text修飾符。

例子:

Button {
    print("Button tapped!")
} label: {
    Text("Tap me").bold()
}

要使文本更大粗,只需鏈接修飾符:

Button {
    print("Button tapped!")
} label: {
    Text("Tap me")
        .font(.title)
        .bold()
}
Button("Tap me") { print("Button tapped!") }.font(.system(size: 30, weight: .bold, design: .default))

您在 macOS 上使用.system(size: )是對的,您需要創建一個PlainButtonStyle按鈕以使文本留在按鈕內。 這就是我的做法

Button("Tap me") {
  print("Button Tapped!")
}.padding()
 .font(.system(size: 20, weight: Font.Weight.bold))
 .foregroundColor(Color.white)
 .background(RoundedRectangle(cornerRadius: 8).fill(Color.blue))
 .buttonStyle(PlainButtonStyle())

如果您使用工具欄並希望確認/添加/保存按鈕為粗體,因為它在 iOS 中是標准的,您只需告訴工具欄您的按鈕是一個確認項。

List {
  // ...
}
.toolbar {
  ToolbarItem(placement: .confirmationAction) {
    Button("Add") {
      // ...
    }
  }
}

您只需將按鈕包裝在ToolbarItem中,其placement: .confirmationAction

這樣,該按鈕將放置在工具欄的右上角,並且也會顯示為粗體

您可以使用.font(.system(size: <font size>))方法增加字體大小。 至於粗體部分,您可以將文本放在兩對*之間。 這使您可以將文本的特定部分或整個內容加粗。

Button(action: {}, 
       label: { 
           Text("Tap **me**").font(.system(size: <font size>))
})
Button(action: {}){
                    Text("Do something")
                        .font(.system(size: 20))
                        .fontWeight(.bold)
                        .foregroundColor(.black)
                    
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM