简体   繁体   中英

SwiftUI how to add an Underline to a Text View?

Is there a way to modify a Text view to have an underline? For example like this text below:

y͟o͟u͟r͟ t͟e͟x͟t͟

Text(Constants.chooseText)
    .font(Font.system(size: 26))
    .foregroundColor(Color.white)
    .padding(.bottom, 80)

Add the underline modifier, on the Text View

        Text("Hello, world!")
            .underline()

UPDATE adding underline() as the first modifier solved the issue.

Text(Constants.chooseText)
    .underline()
    .font(Font.system(size: 26))
    .foregroundColor(Color.white)
    .padding(.bottom, 80)

iOS 16 You can set background and in bg you can set height , offset and color of underline. Sample Code:

Text("Gurjinder Singh")
          .font(.largeTitle)
          .fontWeight(.heavy)
          .background(
             Color.accentColor
                   .frame(height: 6) // underline's height
                   .offset(y: 24) // underline's y pos
           )

Output

在此处输入图像描述

You may use below like:

Text("Forget User ID")
     .underline(true, color: .gray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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