如何为 iOS 应用程序设置 UILabel 的左上角对齐?
我在 nib 文件中添加了一个标签,然后该标签需要左上对齐。由于我在运行时提供文本,因此不确定有多少行。 因此,如果文本仅包含单行,那么它会显示为垂直中心对齐。该对齐方式与我前面各自的标签不匹配。
例如:
这看起来很奇怪:(
有什么方法可以设置标签文本适合左上对齐吗?
I have added one label in my nib file, then its required to have top-left alignment for that lable. As I am providing text at runtime so its not sure that how much lines there are.
So if text contains only single line then it appears as vertical-center aligned. That alignment is not matching with my respective lable in front of it.
For example:
Which is looking odd :(
Is there any way where i can set label text proper to Top-Left alignment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
Swift 5
这很简单,属性的顺序就是一切。
Swift 5
It´s simple, the order of the properties is everything.
我有这个问题,但我的标签在
UITableViewCell
中,并且解决该问题的最简单方法是创建一个空的UIView
并在其中设置标签仅对顶部和左侧进行约束,关闭诅咒时将行数设置为 0I have this problem to but my label was in
UITableViewCell
, and in fund that the easiest way to solve the problem was to create an emptyUIView
and set the label inside it with constraints to the top and to the left side only, on off curse set the number of lines to 0我能够通过将标签嵌入视图中来修复它。效果很完美!
I was able to fix it by embedding the tag within a view. It worked perfect!
您需要在layoutSubviews中设置:
You need to set in layoutSubviews:
如何为 iOS 应用程序设置 UILabel 的左上角对齐?标签将内容模式设置为“左上”对我来说很有效,非常感谢:
How to set top-left alignment for UILabel for iOS application? Label Set Content Mode to "Top Left" work for me, thank you very much:
这很容易做到。使用
verticalAlignment
属性创建一个UILabel
子类,并重写textRectForBounds:limitedToNumberOfLines
以返回顶部、中间或底部垂直对齐的正确边界。代码如下:SOLabel.h
SOLabel.m
It's fairly easy to do. Create a
UILabel
sublcass with averticalAlignment
property and overridetextRectForBounds:limitedToNumberOfLines
to return the correct bounds for a top, middle or bottom vertical alignment. Here's the code:SOLabel.h
SOLabel.m
我将链接到这个相当广泛的内容,而不是重新解释。评价很高的问题/答案:
在 UILabel 中将文本垂直对齐到顶部
简短的答案是否定的,Apple 没有这并不容易,但可以通过更改框架大小来实现。
Rather than re-explaining, I will link to this rather extensive & highly rated question/answer:
Vertically align text to top within a UILabel
The short answer is no, Apple didn't make this easy, but it is possible by changing the frame size.
我在 StoryBoard 中找到了使用 AutoLayout 的解决方案。
1) 将行数设置为 0,并将文本对齐方式设置为左。
2) 设置高度约束。
3) 高度约束应为关系 - 小于或等于
4)
我得到的结果如下:
I found a solution using AutoLayout in StoryBoard.
1) Set no of lines to 0 and text alignment to Left.
2) Set height constraint.
3) The height Constraint should be in Relation - Less Than or Equal
4)
I got the result as follows :
SOLabel 对我有用。
斯威夫特 3 & 5:
该版本已从原始版本更新为允许支持 RTL 语言:
Swift 1:
The SOLabel works for me.
Swift 3 & 5:
This version has been updated from the original to allow support for RTL languages:
Swift 1:
就我而言,这是
底部空间
约束问题。我已将其设置为= 16
。当我将其设置为
bottom to >= 16
时,这个问题得到了解决。另外,如果标签中有任何高度限制,则需要将其删除。
这是尺寸检查器中我的标签的约束视图:
In my case, it was
bottom space
constraint issue. I had set it to= 16
.When I set it to
bottom to >= 16
, this issue got solved.Also, if you have any height constraint in the label, then you need to remove it.
Here's my label's constraint view in the size inspector:
在代码中
请注意,如果您在表格单元格或其他使用不同数据回收的视图中使用它,则需要将原始框架存储在某处并在调用 sizeToFit 之前重置它。
In your code
Beware that if you use that in table cells or other views that get recycled with different data, you'll need to store the original frame somewhere and reset it before calling sizeToFit.
我找到了同一问题的另一个解决方案。我使用
UITextView
而不是UILabel
并将editable()
函数切换为false
。I found another solution for the same problem. I used
UITextView
instead ofUILabel
and switchededitable()
function tofalse
.我也遇到了这个问题,但我发现设置 UILabel 的属性和方法的顺序很重要!
如果您在
label.font = [UIFont fontWithName:@"Helvetica" size:14];
之前调用[label sizeToFit]
,则文本不会与顶部对齐,但如果你交换它们然后就可以了!我还注意到,首先设置文本也会产生影响。
希望这有帮助。
I was also having this problem but what I found was the the order in which you set the UILabel's properties and methods matters!
If you call
[label sizeToFit]
beforelabel.font = [UIFont fontWithName:@"Helvetica" size:14];
then the text does not align to the top but if you swap them around then it does!I also noticed that setting the text first makes a difference too.
Hope this helps.
当您使用界面生成器时,请设置标签的约束(请务必设置高度和宽度)。然后在尺寸检查器中,检查标签的高度。在那里,您将希望它读取 >= 而不是 =。然后在该视图控制器的实现中,将行数设置为 0(也可以在 IB 中完成)并设置标签 [label sizeToFit];随着文本长度的增加,标签的高度也会增加,并使文本保持在左上角。
As you are using the interface builder, set the constraints for your label (be sure to set the height and width as well). Then in the Size Inspector, check the height for the label. There you will want it to read >= instead of =. Then in the implementation for that view controller, set the number of lines to 0 (can also be done in IB) and set the label [label sizeToFit]; and as your text gains length, the label will grow in height and keep your text in the upper left.
如果您需要的是默认从左上角开始的不可编辑文本,您可以简单地使用文本视图而不是标签,然后将其状态设置为不可编辑,如下所示:
比弄乱标签更容易。 ..
干杯!
If what you need is non-editable text that by default starts at the top left corner you can simply use a Text View instead of a label and then set its state to non-editable, like this:
Way easier than messing with the labels...
Cheers!
SoLabel 的解决方案有效,谢谢。
下面我添加了单点触控版本:
Solution with SoLabel works , Thanks.
Bellow I have added monotouch version:
最简单、最简单的方法是在 StackView 中嵌入 Label 并将 StackView 的轴设置为水平,在 Storyboard 的属性检查器中将对齐方式设置为顶部 如下所示< /a>.
The simplest and easiest way is to embed Label in StackView and setting StackView's Axis to Horizontal, Alignment to Top in Attribute Inspector from Storyboard like shown here.
使用UITextView而不是UILabel。它还适用于 UITableViewCell 宽度自动行高
将 isScrollEnabled 和 isEditable 设置为 false。为 TextView 添加最小高度限制
单行文本截图
多行文字截图
Use UITextView instead of UILabel. Also it works for UITableViewCell width automatic row height
Set isScrollEnabled and isEditable to false. Add min height constraint for TextView
One line text screenshot
Multiline text screenshot
在 totiG 的出色答案的基础上,我创建了一个 IBDesignable 类,可以非常轻松地从 StoryBoard 自定义 UILabel 的垂直对齐方式。只需确保从 StoryBoard 身份检查器将 UILabel 的类设置为“VerticalAlignLabel”即可。如果垂直对齐没有生效,请转到编辑器->刷新所有视图,这应该可以解决问题。
工作原理:
正确设置 UILabel 的类后,故事板应该向您显示一个采用整数(对齐代码)的输入字段。
更新:我添加了对居中标签的支持 ~Sev
输入 0 表示顶部对齐
输入 1 表示中间对齐
输入 2 表示底部对齐
Building on top of totiG's awesome answer, I have created an IBDesignable class that makes it extremely easy to customize a UILabel's vertical alignment right from the StoryBoard. Just make sure that you set your UILabel's class to 'VerticalAlignLabel' from the StoryBoard identity inspector. If the vertical alignment doesn't take effect, go to Editor->Refresh All Views which should do the trick.
How it works:
Once you set your UILabel's class correctly, the storyboard should show you an input field that takes an integer (alignment code).
Update: I've added support for centered labels ~Sev
Enter 0 for Top Alignment
Enter 1 for Middle Alignment
Enter 2 for Bottom Alignment
只需覆盖
drawText
并使用textRect(forBounds:limitedToNumberOfLines:)
所以:
Simply override
drawText
and usetextRect(forBounds:limitedToNumberOfLines:)
So:
@totiG 答案的 Swift 3 版本
Swift 3 version of @totiG's answer
您也可以将 UILabel 更改为 UITextView,因为它们基本上做相同的事情,除了 UITextView 的优点是文本自动对齐到左上角
you can also just change your UILabel to UITextView, because they basically do the same thing except the advantage of UITextView is that text is automatically aligned to the top left
@totiG 的答案是正确的并解决了我的问题。但我在实现这个方法时发现了一个问题,在像5s、SE这样的较小设备上,这对我来说不起作用。我必须在
override func layoutSubViews()
中设置label.sizeToFit()
@totiG's answer is correct and solved my problem. But I found a problem while implementing this method, in smaller devices like 5s , SE, this doesn't work for me. I have to set
label.sizeToFit()
inoverride func layoutSubViews()
纯故事板解决方案...
水平左对齐很容易,因为它可以在 Interface Builder 中设置为 UILabel 属性。
垂直顶部对齐只需两个约束即可设置:
一个将 UILabel.bottom 设置为
大于或等于
某个其他视图(最大高度),优先级为 1000(必需)。这将增加 UILabel 增长到该约束高度的能力其他设置 UILabel.bottom 为
equal
0(最小高度),优先级为 750(非必需)。这将增加 UILabel 缩小到其内容允许的最小尺寸的能力/趋势这意味着 UIKit 将尝试根据其属性/内容(文本、字体、. ..) 最大为 1. 约束高度。
Pure Storyboard solution...
Horizontal left alignment is easy since it can be setup as UILabel property in Interface Builder.
Vertical top alignment can be setup with just two constraints:
one that setups UILabel.bottom as
greater or equal
to some other view (maximal height) with priority like 1000 (required). This will add UILabel ability to grow up to this constraint's heightother that setups UILabel.bottom like
equal
0 (minimal height) with priority like 750 (non-required). This will add UILabel ability/tendency to shrink to minimal size allowed by its contentThis means that UIKit will try to setup UILabel height as minimal as possible in range from minimal auto-sized UILabel based on its properties/content (text, font, ...) up to 1. constraint height.
对于 iOS 7,这就是我所做的并为我工作
For iOS 7 that's what i made and worked for me
Swift 2.0::使用 UILabel 扩展
在空 Swift 文件中创建常量枚举值。
使用 UILabel 扩展:
创建一个空的 Swift 类并为其命名。添加以下内容。
用法 :
Swift 2.0: : Using UILabel Extension
Make constant enum values in a empty Swift file.
Using UILabel Extension:
Make a empty Swift class and name it. Add the following.
Usage :