根据布局方向翻转 SwiftUI 形状

sal*_*000 6 localization ios swift swiftui

我制作了一个 SemiRoundedRectangle 形状,用于对侧面菜单进行 ClipShape。如果用户使用 RTL 语言,我需要翻转它,但不确定实现此目的的最佳方法。

\n

我已经尝试过.flipsForRightToLeftLayoutDirection(true),但这也会翻转实际的阿拉伯文本。当我尝试旋转形状时,它不再符合形状协议,因此我不能再在.clipShape. 当我切换到阿拉伯语时,SwiftUI 中的其他所有内容都会神奇地自行翻转,是否可以在我的形状中添加一些东西来赋予它这些魔力?

\n

感谢您的帮助 :)

\n
\nimport SwiftUI\n\nstruct SemiRoundedRectangle: Shape {\n    var cornerRadius: CGFloat\n    func path(in rect: CGRect) -> Path {\n        var path = Path()\n        path.move(to: CGPoint(x: rect.maxX, y: rect.minY))\n        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))\n        path.addLine(to: CGPoint(x: rect.minX+cornerRadius, y: rect.maxY))\n        path.addArc(center: CGPoint(x: cornerRadius, y: rect.height - cornerRadius),\n                    radius: cornerRadius,\n                    startAngle: .degrees(90),\n                    endAngle: .degrees(180), clockwise: false)\n        path.addLine(to: CGPoint(x: 0, y: cornerRadius))\n        path.addArc(center: CGPoint(x: cornerRadius, y: cornerRadius),\n                    radius: cornerRadius,\n                    startAngle: .degrees(180),\n                    endAngle: .degrees(270), clockwise: false)\n        path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))\n        \n        return path\n    }\n\n}\n\nstruct TestView {\n    var body: some View {\n        HStack {\n            Text("\xd8\xa7\xd9\x8a\xd9\x87 \xd8\xa7\xd9\x84\xd8\xa3\xd8\xae\xd8\xa8\xd8\xa7\xd8\xb1\xd8\x9f")\n            .padding()\n            .background(Color.green)\n            .clipShape(SemiRoundedRectangle(cornerRadius: 10.0))\n            \n            Spacer()\n        }\n        \n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

ahe*_*eze 2

尝试将 附加clipShapeColor.green

\n
struct TestView: View {\n    var body: some View {\n        HStack {\n            Text("\xd8\xa7\xd9\x8a\xd9\x87 \xd8\xa7\xd9\x84\xd8\xa3\xd8\xae\xd8\xa8\xd8\xa7\xd8\xb1\xd8\x9f")\n            .padding()\n            .background(\n                Color.green /// here!\n                    .clipShape(SemiRoundedRectangle(cornerRadius: 10.0))\n                    .flipsForRightToLeftLayoutDirection(true)\n            )\n            \n            Spacer()\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n
英语RTL语言
左角圆角的绿色矩形,位于屏幕左侧右角圆角的绿色矩形,位于屏幕右侧
\n