How to scale text to fit parent view with SwiftUI?

前端 未结 6 860
-上瘾入骨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 09:48

    You want to allow your text to:

    • shrink up to a certain limit
    • on 1 (or several) line(s)

    You choose this scale factor limit to suit your need. Typically you don't shrink beyond readable or beyond the limit that will make your design look bad

    struct ContentView : View {
    var body: some View {
        ZStack {
            Circle().strokeBorder(Color.red, lineWidth: 30)
            Text("Text")
                .scaledToFill()
                .minimumScaleFactor(0.5)
                .lineLimit(1)
        }
    }
    

    }

提交回复
热议问题