I'm writing an app for iPhone using SwiftUI in XCode. In one of the views, there is a Text label that changes its text whenever a button is pressed. The entire view is spring animated, so whenever the text is changed via the button, it is changed with an animation. The animation works well, except during the animation the Text label adds an unnecessary ellipsis to the end of the text. I've tried to remove the ellipsis using:
Text("text")
.truncationMode(nil)
However, this gives an error. Is there any way to .turn off the "..." in the Text label? If not, is there a way to turn off animations for just that Text label without affecting the others, since the entire view is animated?
SwiftUI has wonderful animation features, but the way it handles changes in Text View content is problematic. It animates the change of the text frame but changes the text immediately without animation.
The animation modifier applies to all animatable changes within the views it wraps. Change the animation type from easeInOut to spring (). SwiftUI includes basic animations with predefined or custom easing, as well as spring and fluid animations.
SwiftUI includes basic animations with predefined or custom easing, as well as spring and fluid animations. You can adjust an animation’s speed, set a delay before an animation starts, or specify that an animation repeats. Try turning off animation for the rotation by adding another animation modifier just above the scaleEffect modifier.
When using SwiftUI, you can individually animate changes to views, or to a view’s state, no matter where the effects are. SwiftUI handles all the complexity of these combined, overlapping, and interruptible animations for you.
Try this:
struct UnAnimatedText: View {
private let text: String
init(_ text: String) {
self.text = text
}
var body: some View {
Button(action: {
}) {
Text(text)
.frame(maxWidth: .infinity)
.animation(nil)
}
.disabled(true)
}
}
The text will change without animation but frame of UnAnimatedText - with animations.
.frame(maxWidth: .infinity) is optional, the main idea is to wrap in Button
You can use minimumScaleFactor(_ factor: CGFloat)
. The text will shrink according to the factor value.
For example, if your font size is 10 and your factor is 0.4, the text font size will be able to decrease down to 4 if needed.
Text("text")
.minimumScaleFactor(0.1)
You can use Text("text").animation(nil)
to turn off animation.
or you can choose other animations to prevent the ...
Text("text").animation(.spring(response: 0.0, dampingFraction:0.2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With