How to scale text to fit parent view with SwiftUI?

前端 未结 6 954
-上瘾入骨i
-上瘾入骨i 2021-02-02 09:41

I\'d like to create a text view inside a circle view. The font size should be automatically set to fit the size of the circle. How can this be done in SwiftUI? I tried scaledToF

6条回答
  •  轮回少年
    2021-02-02 10:06

    To achieve this you don't need the ZStack. You can add a background to the Text:

    Text("Text text text?")
        .padding()
        .background(
           Circle()
              .strokeBorder(Color.red, lineWidth: 10)
              .scaledToFill()
              .foregroundColor(Color.white)
        )
    

    The result is this:

提交回复
热议问题