How SwiftUI sets the spacing between the underline and the text?

匆匆过客 提交于 2020-12-06 11:22:10

问题


Code to set underline,I want to make the space between the text and the underline larger.

 Text("underline text")
 .underline()

回答1:


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.")
    }
}



回答2:


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)



回答3:


How about use a custom view instead of .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)


来源:https://stackoverflow.com/questions/63238842/how-swiftui-sets-the-spacing-between-the-underline-and-the-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!