简体   繁体   English

SwiftUI如何设置下划线与文字的间距?

[英]How SwiftUI sets the spacing between the underline and the text?

Code to set underline,I want to make the space between the text and the underline larger.代码要设置下划线,我想让文字和下划线之间的空间更大。

 Text("underline text")
 .underline()

Underline is a font feature, you can do custom under just by drawing line anywhere needed下划线是一种字体功能,您可以在任何需要的地方画线来自定义下划线

演示

var body: some View {
    HStack {
        Text("Before")
        Text("underline text")
            .overlay(
                Rectangle().frame(height: 1).offset(y: 4)
                , alignment: .bottom)
        Text("after.")
    }
}

How about use a custom view instead of.underline?使用自定义视图而不是.underline 怎么样?

struct MyUnderline: View {
      let color: Color = .black
      let height: CGFloat = 1
      var body: some View {
          Rectangle()
            .fill(color)
            .frame(height: height)
      }
}    
Text("underline text")
MyUnderline()
  .padding(.top, -10)

You could create a custom view that takes the text and underline padding as parameters您可以创建一个自定义视图,将文本和下划线填充作为参数

struct UnderlinedText: View {

   var text: String
   var underlinePadding: CGFloat

   var body: some View {
       VStack (spacing: underlinePadding) {
           Text(text)
           GeometryReader { proxy in
               Rectangle()
                   .frame(width: proxy.size.width, height: 1)
           }
        }
    }
}

And use it as follows并按如下方式使用

UnderlinedText(text: "Hello underlined text", underlinePadding: 10.0)

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

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