autolayout-conform UILabel 与垂直文本(objC 或 Swift)?
我将如何创建一个具有垂直文本流的 UIView / UILabel ,它看起来像这个示例屏幕的红色视图?
我读过有关 view.transform = CGAffineTransform(... 的内容,它允许轻松旋转,但它会打破自动布局约束。
我很乐意使用第三方库,但是我找不到 任何。
How would I create an UIView / UILabel with vertical text flow which would look like the red view of this example screen?
I have read about view.transform = CGAffineTransform(... which allows for easy rotation, BUT it would break the auto-layout constraints.
I would be happy to use a third-party library, but I cannot find any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Apple 的文档中所述:
因此,为了让转换后的视图与自动布局“发挥良好”作用,我们实际上需要告诉约束使用相反的轴。
例如,如果我们在
UIView
中嵌入UILabel
并将标签旋转 90 度,我们想要限制“容器”视图的宽度到标签的高度,其高度到标签的宽度。这是一个示例
VerticalLabelView
视图子类:我添加了一些属性以允许将视图视为标签,因此我们可以这样做:
当然,这可以扩展为“通过”我们需要使用的所有标签属性,因此我们不需要直接引用
.label
。现在,这个
VerticalLabelView
的使用方式与普通UILabel
非常相似。下面是两个示例 - 它们都使用此
BaseVC
来设置视图:第一个示例 -
SubviewsExampleVC
- 将每个示例添加为子视图,然后我们在视图之间添加约束:第二个示例 = StackviewExampleVC - 将每个示例添加为 UIStackView 的排列子视图:
两个示例都会生成以下输出:
请注意:这是仅示例代码 - 它无意也不应被视为生产就绪
As noted in Apple's docs:
So, to get transformed views to "play nice" with auto layout, we need to - in effect - tell constraints to use the opposite axis.
For example, if we embed a
UILabel
in aUIView
and rotate the label 90-degrees, we want to constrain the "container" view's Width to the label's Height and its Height to the label's Width.Here's a sample
VerticalLabelView
view subclass:I've added a few properties to allow the view to be treated like a label, so we can do:
This could, of course, be extended to "pass through" all label properties we need to use so we wouldn't need to reference the
.label
directly.This
VerticalLabelView
can now be used much like a normalUILabel
.Here are two examples - they both use this
BaseVC
to setup the views:The first example -
SubviewsExampleVC
- adds each as a subview, and then we add constraints between the views:The second example =
StackviewExampleVC
- adds each as an arranged subview of aUIStackView
:Both examples produce this output:
Please note: this is Example Code Only - it is not intended to be, nor should it be considered to be, Production Ready