[英]How to bold text in TextField swiftUI?
There is no builtin font-weight modifier for textfield in SwiftUI (XCODE: 11.2.1) . SwiftUI (XCODE: 11.2.1)中的文本字段没有内置的字体粗细修饰符。 How can we introduce font-weight without extending UITextField as UIViewRepresentable?
我们如何在不将 UITextField 扩展为 UIViewRepresentable 的情况下引入字体粗细?
A general approach for using standard font size options and weights that work with SwiftUI TextField.使用适用于 SwiftUI TextField 的标准字体大小选项和粗细的一般方法。 For example:
例如:
TextField("Name", text: $name)
.font(Font.headline.weight(.light))
Available standard size options (smallest to largest):可用的标准尺寸选项(最小到最大):
.caption
.footnote
.subheadline
.callout
.body
.headline
.title
.largeTitle
Available standard font weights (lightest to heaviest):可用的标准字体粗细(从轻到重):
.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)
Expanding on shawnynicole's answer , you can create an extension:扩展shawnynicole 的回答,您可以创建一个扩展:
extension View {
func bold() -> some View {
font(Font.body.bold())
}
}
and apply it to any View (including the TextField
):并将其应用于任何视图(包括
TextField
):
TextField("Text", text: $text)
.bold()
Foundation supports markdown in Swift 5.5 Foundation 在 Swift 5.5 中支持 Markdown
Add double asterisks (**) arroud the text/characters to make it bold.在文本/字符周围添加双星号 (**) 以使其加粗。
Text("**This text is bold**")
To emphasize text, use underscore要强调文本,请使用下划线
Text("_This text is italic_")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.