[英]How to bold text in TextField swiftUI?
SwiftUI (XCODE: 11.2.1)中的文本字段沒有內置的字體粗細修飾符。 我們如何在不將 UITextField 擴展為 UIViewRepresentable 的情況下引入字體粗細?
使用適用於 SwiftUI TextField 的標准字體大小選項和粗細的一般方法。 例如:
TextField("Name", text: $name)
.font(Font.headline.weight(.light))
可用的標准尺寸選項(最小到最大):
.caption
.footnote
.subheadline
.callout
.body
.headline
.title
.largeTitle
可用的標准字體粗細(從輕到重):
.ultralight
.thin
.light
.regular
.medium
.semibold
.bold
.heavy
.black
import SwiftUI
struct ContentView: View {
@State var TextValue: String = "Hello"
var body: some View {
VStack {
TextField("placeholder", text: $TextValue)
.padding(.horizontal, 50)
.font(.system(size: 30, weight: .heavy, design: .default))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
TextField("Name", text: $name)
.font(Font.body.bold())
iOS 13.0+、macOS 10.15+、tvOS 13.0+、watchOS 6.0+ 的更新方法是:
.fontWeight(.bold)
擴展shawnynicole 的回答,您可以創建一個擴展:
extension View {
func bold() -> some View {
font(Font.body.bold())
}
}
並將其應用於任何視圖(包括TextField
):
TextField("Text", text: $text)
.bold()
Foundation 在 Swift 5.5 中支持 Markdown
在文本/字符周圍添加雙星號 (**) 以使其加粗。
Text("**This text is bold**")
要強調文本,請使用下划線
Text("_This text is italic_")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.