顶部对齐的 UILabel,使文本贴在标签视图的顶部 -ios

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11867135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:39:07  来源:igfitidea点击:

Top aligned UILabel, make text stick to top of label view -ios

iphoneobjective-ciosxcode

提问by Mrwolfy

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?

有没有办法“顶部对齐”UILabel,即让文本贴在标签视图的顶部?与垂直居中对齐或浮动在视图中心相反,这是默认设置?

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?

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

enter image description here

在此处输入图片说明

采纳答案by Paul O.

There is no way to set the vertical alignment of a UILabelby default in iOS. Instead, you will want to use one of the sizeWithFontmethods 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 fontproperty of the label in question. You can look herefor some more details on using these functions.

UILabel在 iOS 中,默认情况下无法设置 a 的垂直对齐方式。相反,你将要使用的一个sizeWithFont由提供的方法NSString。您使用哪一种取决于您想要多行标签还是单行标签。sizeWithFont:forWidth:lineBreakMode:可用于单行标签,sizeWithFont:constrainedToSize:lineBreakMode:也可用于多行标签。您可以使用font相关标签的属性轻松加载字体参数。您可以在此处查看有关使用这些功能的更多详细信息。

These methods will return a CGSizestruct 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.

这些方法将CGSize根据NSString. 一旦您拥有正确的标签大小,您就可以轻松地将其放置在您的超级视图的顶部,并且它看起来是顶部对齐的。

回答by AboulEinein

There's a workaround for it:

有一个解决方法:

  1. Set a height & width constraintto your UILabelwith a constant <= to the maximum height|width you want.
  2. Set number of linesto zero.
  3. Set the height in storyboardto fit only one line.
  4. In your code after setting the text of UILabelcall sizeToFit.
  1. 将高度和宽度设置constraint为您UILabel的常量 <= 到您想要的最大高度|宽度。
  2. 设置number of lines为零。
  3. 将高度设置为storyboard仅适合一行。
  4. 在设置UILabelcall文本后的代码中sizeToFit

This would make your UILabelto resize within the bounds of the maximum width & height you want and text will always be top aligned.

这将使您UILabel在所需的最大宽度和高度的范围内调整大小,并且文本将始终顶部对齐。

回答by Mrwolfy

I used the answer linked above from nevan kingin a comment to my question: Vertically align text to top within a UILabel

我在对我的问题的评论中使用了nevan king上面链接的答案:Vertically align text to top inside a UILabel

The code I used is here:

我使用的代码在这里:

- (void)setUILabelTextWithVerticalAlignTop:(NSString *)theText {
    CGSize labelSize = CGSizeMake(250, 300);
    CGSize theStringSize = [theText sizeWithFont:targetLabel.font constrainedToSize:labelSize lineBreakMode:targetLabel.lineBreakMode];
    targetLabel.frame = CGRectMake(targetLabel.frame.origin.x, targetLabel.frame.origin.y, theStringSize.width, theStringSize.height);
    targetLabel.text = theText;
}

//linked to a button that sets the text from a UITextView to the label.
- (IBAction)setText:(id)sender {

    [sourceText resignFirstResponder];
    [self setUILabelTextWithVerticalAlignTop:sourceText.text];
    [targetLabel setNumberOfLines:0];
    [targetLabel sizeToFit];

}

Here is the project:

这是项目:

owolf.net/uploads/StackOverflow/verticalAlignUILabel.zip

owolf.net/uploads/StackOverflow/verticalAlignUILabel.zip

回答by WebOrCode

I have replaced UILabel with UITextView, because in UITextView, the text sticks to the top.

我已经用 UITextView 替换了 UILabel,因为在 UITextView 中,文本粘在顶部。

Just a suggestion, it may be useful for someone.

只是一个建议,它可能对某人有用。

回答by Wodjefer

Make a subclass of UILabel

创建 UILabel 的子类

.h file

.h 文件

@property (nonatomic, assign) UIControlContentVerticalAlignment verticalAlignment;

.m file

.m 文件

- (void) drawTextInRect:(CGRect)rect 
{
    if(_verticalAlignment == UIControlContentVerticalAlignmentTop ||  _verticalAlignment == UIControlContentVerticalAlignmentBottom)    
    {
         // If one line, we can just use the lineHeight, faster than querying sizeThatFits
         const CGFloat height = floorf(((self.numberOfLines == 1) ? ceilf(self.font.lineHeight) : [self sizeThatFits:self.frame.size].height));
          rect.origin.y = floorf(((self.frame.size.height - height) / 2.0f) * ((_verticalAlignment == UIControlContentVerticalAlignmentTop) ? -1.0f : 1.0f));
    }
    [super drawTextInRect:rect];
}


- (void) setVerticalAlignment:(UIControlContentVerticalAlignment)newVerticalAlignment
{
     _verticalAlignment = newVerticalAlignment;
     [self setNeedsDisplay];
}

I used this for Vertical Alignment .

我将此用于垂直对齐。

回答by Alexander Poleschuk

There is an easy solution for cases where the height of a label doesn't need to be constant: put your Labelin a Stack View. Be sure to add leading and trailing constants to the Stack View. Here is a screenshot of how to do it in storyboard:

对于标签高度不需要恒定的情况,有一个简单的解决方案:将您LabelStack View. 确保将前导和尾随常量添加到Stack View. 这是如何在故事板中执行此操作的屏幕截图:

enter image description here

在此处输入图片说明

回答by Hyman Tiong

What I did in my app was to set the UILabel's line property to 0 as well as to create a bottom constraint of the UILabeland make sure it is being set to >= 0 as shown in the image below.

我在我的应用程序中所做的是将 UILabel 的 line 属性设置为 0 以及创建 的底部约束,UILabel并确保将其设置为 >= 0,如下图所示。

enter image description here

在此处输入图片说明