Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Adding Unnecessary "dot dot dot" to Animated Text

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?

like image 598
billschmidt626 Avatar asked Dec 01 '19 03:12

billschmidt626


People also ask

How does SwiftUI handle changes in text view?

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.

What is the animation modifier in SwiftUI?

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.

How do I Turn Off animation for the rotation in SwiftUI?

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.

Why should I use SwiftUI?

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.


3 Answers

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

like image 87
Valitov Azamat Avatar answered Oct 22 '22 17:10

Valitov Azamat


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)
like image 40
Nicolas Mandica Avatar answered Oct 22 '22 16:10

Nicolas Mandica


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))
like image 31
E.Coms Avatar answered Oct 22 '22 15:10

E.Coms