html 浮动文本_swiftui中更简单的更好的浮动标签文本字段

html 浮动文本

There are certainly opinions and opinions about floating label text fields. This article is not about whether or when to use them. But if you do need to build some, it couldn’t be easier than with SwiftUI!

Ť这里有一定的意见看法有关浮动标签的文本字段。 本文与是否或何时使用它们无关。 但是,如果您确实需要构建一些组件,那么它与SwiftUI一样简单!

Let’s start with the simplest possible approach: let’s vertically stack a caption text above a text field. We’ll use exactly the same adaptive color for the caption that SwiftUI (UIKit actually) uses for placeholders, UIColor.placeholderText (SwiftUI’s Color has a constructor that takes a UIColor, so we’re good to go).

让我们从最简单的方法开始:让我们在文本字段上方垂直堆叠标题文本。 我们将使用完全相同的自适应颜色为SwiftUI(实际上的UIKit)占位符的使用,标题UIColor.placeholderText (SwiftUI的Color有一个构造函数需要UIColor ,所以我们好去)。

Let’s take our fledgling FloatingTextField for a spin with this example code:

让我们用这个示例代码来试一下我们刚起步的FloatingTextField

Image for post

This already starts to look nice, but there’s that duplicated placeholder text. We know that the TextField hides its placeholder as soon as its text is not empty, so let’s animate our little placeholder into place when that happens. We’ll initially give it a slight vertical offset so that it rises up as it appears, and set its initial opacity to zero. We’ll also add a default animation between the two states (text empty and non-empty).

这已经开始看起来不错,但是有重复的占位符文本。 我们知道,一旦文本字段不为空, TextField隐藏其占位符,因此,当发生这种情况时,让我们将小占位符设置为动画。 最初,我们将为它提供一个小的垂直偏移,以使其随其出现而上升,并将其初始不透明度设置为零。 我们还将在两个状态(文本为空和非空)之间添加默认动画。

Image for post

Not too bad for such little code! But if you look closely, you’ll notice that the placeholder doesn’t actually animate. We just faded a new one in as the TextField’s built-in placeholder disappeared.

对于这么小的代码来说还不错! 但是如果仔细观察,您会发现占位符实际上并没有动画。 当TextField的内置占位符消失时,我们刚刚淡入一个新的。

If we want a little more perfection, we’ll have to replace the default placeholder. We’ll pass an empty placeholder to the TextView and overlay our own with a ZStack. Then, we’ll scale it down into place.

如果我们想要更多的完善,我们将不得不替换默认的占位符。 我们将一个空的占位符传递给TextView并用ZStack覆盖我们自己的占位符。 然后,我们将其缩小到位。

Note that we added a little top padding to make place for the scaled-down placeholder. We also made the animation a little more playful with a spring.

请注意,我们添加了一些顶部填充以缩小比例的占位符。 我们还通过春天使动画更具趣味性。

Image for post

You can even animate the color of the placeholder, like so:

您甚至可以为占位符的颜色设置动画,如下所示:

Image for post

Have fun tweaking the animations to your taste and for your use-case! 🎉

有趣的是,可以根据自己的喜好和用例来调整动画! 🎉

翻译自: https://medium.com/swlh/simpler-better-floating-label-textfields-in-swiftui-24f7d06da8b8

html 浮动文本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值