I’ve found a workaround that avoids the truncation issue on iOS 18 and later using the new textRenderer(_:) modifier.
It ensures that all lines are rendered properly without truncation.
@available(iOS 17, *)
struct MyTextRenderer: TextRenderer {
func draw(layout: Text.Layout, in ctx: inout GraphicsContext) {
for line in layout {
ctx.draw(line)
}
}
}
extension View {
@ViewBuilder
func avoidTextTruncationBug() -> some View {
if #available(iOS 18, *) {
textRenderer(MyTextRenderer())
} else {
self
}
}
}
struct MessageTextView: View {
var text: String
var body: some View {
Text(text)
.fixedSize(horizontal: false, vertical: true)
.font(.body)
.padding(.leading, 16)
.padding(.trailing, 16)
.padding(.top, 8)
.padding(.bottom, 8)
.avoidTextTruncationBug()
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: