1

我似乎无法掌握如何确保我的文本覆盖并适合圆形图像的边界。这就是我到目前为止所拥有的。

Circle()
    .frame(width: 100, height: 100)
    .overlay(
        Text("Reaaaallllllyyyyyy Looooooongggggg")
            .foregroundColor(Color.red)
            .frame(minWidth: 0, maxWidth: .infinity)
        )

覆盖在圆圈顶部的文本

SwiftUI 中有没有办法让它工作?我将如何实现这一目标?

4

1 回答 1

2
struct ClippedCircleView: View {
    @State var text: String = "Reaaaallllllyyyyyy Looooooongggggg"
    // Make this any number you are comfortable with
    @State var letterLimit: Int = 12
    var body: some View {
        
        Circle()
            .frame(width: 100, height: 100)
            .overlay(
                Text(text)
                    //Limit to 1 line until your letterLimit
                    .lineLimit(text.count <= letterLimit ? 1 : nil)
                    //Clips it to shape
                    .clipShape(ContainerRelativeShape()).padding()
                    .foregroundColor(Color.red)
                    //This makes super tiny letters so adjust to your use case
                    .minimumScaleFactor(0.1)
                
            )
    }
}
于 2021-04-10T00:05:38.590 回答