I need implement next flow in my code: Show text(1 line) and if it is cut or clipped (not fully visible) I should remove them, I don't want scale text or make it in 2 lines
Asked
Active
Viewed 760 times
1
-
1What have you tried so far? – Tushar Sharma Feb 23 '21 at 10:01
-
I tried: geometry and text lenght calculation (using UIKit) but it's tons of code and perfomance is sad – UnRewa Feb 23 '21 at 11:54
-
This might help you: [How we can get and read size of a Text with GeometryReader in SwiftUI?](https://stackoverflow.com/q/64452647/8697793) – pawello2222 Feb 23 '21 at 15:32
1 Answers
1
so I found solution, this solution is compose from few different topics:
struct TruncatableText: View {
let text: (() -> Text)
let lineLimit: Int?
@State private var intrinsicSize: CGSize = .zero
@State private var truncatedSize: CGSize = .zero
@State private var hide: Bool = false
var body: some View {
text()
.lineLimit(lineLimit)
.readSize { size in
truncatedSize = size
hide = truncatedSize != intrinsicSize
}
.background(
text()
.fixedSize(horizontal: false, vertical: true)
.hidden()
.readSize { size in
intrinsicSize = size
hide = truncatedSize != intrinsicSize
}
)
.isShow(!hide)
}
}
extension View {
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
}
)
.onPreferenceChange(SizePreferenceKey.self, perform: onChange)
}
@ViewBuilder func isShow(_ show: Bool) -> some View {
if show {
self
} else {
self.hidden()
}
}
}
struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {}
}
and using it:
TruncatableText(
text: {
Text("title")
.font(.system(size: 21, weight: .semibold))
.foregroundColor(Color.blue)
},
lineLimit: 1
)

UnRewa
- 2,462
- 2
- 29
- 31