Positioning View using anchor point

前端 未结 3 1600
遥遥无期
遥遥无期 2021-01-02 19:25

I have several dozen Texts that I would like to position such that their leading baseline (lastTextBaseline) is at a specific coordinate. position

3条回答
  •  清酒与你
    2021-01-02 20:02

    Updated: you could try the following variants

    Letter at point

    let font = UIFont.systemFont(ofSize: 48)
    var body: some View {
        ZStack {
            ForEach(locations) { run in
                Text(verbatim: run.string)
                    .font(Font(self.font))
                    .border(Color.green)
                    .offset(x: 0, y: -self.font.lineHeight / 2.0)
                    .rotationEffect(.radians(run.angle))
                    .position(run.point)
    
                Circle()  // Added to show where `position` is
                    .frame(maxWidth: 5)
                    .foregroundColor(.red)
                    .position(run.point)
            }
        }
    }
    

    there is also next interesting variant, use ascender instead of above lineHeight

    .offset(x: 0, y: -self.font.ascender / 2.0)
    

    enter image description here

提交回复
热议问题