顶部对齐的UILabel,使文本粘贴到标签视图顶部-ios [英] Top aligned UILabel, make text stick to top of label view -ios

查看:100
本文介绍了顶部对齐的UILabel,使文本粘贴到标签视图顶部-ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法顶部对齐UILabel,即文本是否粘贴到标签视图的顶部?与中心垂直对齐相反,或浮动在视图的中心,默认为?

Is there a way to "top align" a UILabel, that is make text stick to top of label view? As opposed to center aligned vertically, or float in the center of the view, as is the default?

这是三个标签的图像,左右对齐,以及与中心和顶部对齐的UITextView。无论视图的垂直大小如何,textView的文本都会粘到顶部。我可以用标签做同样的事情吗?

Here is an image of three labels, aligned left, right and center, and a UITextView that is aligned center and top. The text of the textView sticks to the top regardless of the vertical size of the view. Can I do the same thing with a label?

推荐答案

无法设置 UILabel的垂直对齐默认情况下在iOS中。相反,您将需要使用 NSString 提供的 sizeWithFont 方法之一。您使用哪一个取决于您是否需要多行或单行标签。 sizeWithFont:forWidth:lineBreakMode:可用于单行标签,而 sizeWithFont:constrainedToSize:lineBreakMode:可以是用于多行标签。您可以使用相关标签的 font 属性轻松加载font参数。您可以在此处查看关于使用这些函数的更多细节。

There is no way to set the vertical alignment of a UILabel by default in iOS. Instead, you will want to use one of the sizeWithFont methods provided by NSString. Which one you use depends on whether you want a multi-line or single-line label. sizeWithFont:forWidth:lineBreakMode: can be used for single-line labels, while sizeWithFont:constrainedToSize:lineBreakMode: can be used for multi-line labels. You can easily load the font parameter with the font property of the label in question. You can look here for some more details on using these functions.

这些方法将返回一个 CGSize 结构,其中包含标签的最小尺寸基于 NSString 中的文本。一旦您拥有正确的标签尺寸,您就可以轻松地将其放置在超级视图的顶部,并且它看起来是顶部对齐的。

These methods will return a CGSize struct with the minimum size for your label based on the text in the NSString. Once you have the correct size for the label, you can easily place it at the top of your superview and it will appear to be top-aligned.

这篇关于顶部对齐的UILabel,使文本粘贴到标签视图顶部-ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆