簡體   English   中英

SwiftUI如何設置下划線與文字的間距?

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

代碼要設置下划線,我想讓文字和下划線之間的空間更大。

 Text("underline text")
 .underline()

下划線是一種字體功能,您可以在任何需要的地方畫線來自定義下划線

演示

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

使用自定義視圖而不是.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)

您可以創建一個自定義視圖,將文本和下划線填充作為參數

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

並按如下方式使用

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

暫無
暫無

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

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