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

时间:2023-01-22 17:42:08

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的文本都将粘在顶部。我能用标签做同样的事情吗?

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

7 个解决方案

#1


8  

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.

在iOS中,无法默认设置UILabel的垂直对齐。相反,您将希望使用NSString提供的sizeWithFont方法之一。您使用哪一个取决于您想要多行还是单行标签。sizeWithFont:forWidth:lineBreakMode:可以用于单行标签,而sizeWithFont:constrainedToSize:lineBreakMode:可以用于多行标签。您可以轻松地使用所涉标签的字体属性加载字体参数。您可以在这里查看关于使用这些函数的更多细节。

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.

这些方法将返回一个CGSize结构体,根据NSString中的文本为标签提供最小大小。一旦标签有了正确的大小,就可以很容易地将它放在父视图的顶部,并且它看起来是顶对齐的。

#2


25  

There's a workaround for it:

有一个变通的办法:

  1. Set a height & width constraint to your UILabel with a constant <= to the maximum height|width you want.
  2. 为UILabel设置一个高度和宽度约束,常量<=您想要的最大高度|宽度。
  3. Set number of lines to zero.
  4. 将行数设置为零。
  5. Set the height in storyboard to fit only one line.
  6. 将故事板中的高度设置为只适合一行。
  7. In your code after setting the text of UILabel call sizeToFit.
  8. 在您的代码中设置了UILabel的文本后调用sizeToFit。

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

这将使你的UILabel调整到你想要的最大宽度和高度的范围内,并且文本始终是顶部对齐的。

#3


12  

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

我用了nevan king的答案来回答我的问题:将文本垂直对齐到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/*/verticalAlignUILabel.zip

owolf.net/uploads/*/verticalAlignUILabel.zip

#4


6  

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.

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

#5


1  

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 .

我用它来做垂直对齐。

#6


1  

There is an easy solution for cases where the height of a label doesn't need to be constant: put your Label in 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:

对于标签的高度不需要是常量的情况,有一个简单的解决方案:将标签放在堆栈视图中。一定要在堆栈视图中添加引导和尾随常量。下面是如何在故事板中实现的截图:

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

#7


0  

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 UILabel and make sure it is being set to >= 0 as shown in the image below.

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

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

#1


8  

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.

在iOS中,无法默认设置UILabel的垂直对齐。相反,您将希望使用NSString提供的sizeWithFont方法之一。您使用哪一个取决于您想要多行还是单行标签。sizeWithFont:forWidth:lineBreakMode:可以用于单行标签,而sizeWithFont:constrainedToSize:lineBreakMode:可以用于多行标签。您可以轻松地使用所涉标签的字体属性加载字体参数。您可以在这里查看关于使用这些函数的更多细节。

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.

这些方法将返回一个CGSize结构体,根据NSString中的文本为标签提供最小大小。一旦标签有了正确的大小,就可以很容易地将它放在父视图的顶部,并且它看起来是顶对齐的。

#2


25  

There's a workaround for it:

有一个变通的办法:

  1. Set a height & width constraint to your UILabel with a constant <= to the maximum height|width you want.
  2. 为UILabel设置一个高度和宽度约束,常量<=您想要的最大高度|宽度。
  3. Set number of lines to zero.
  4. 将行数设置为零。
  5. Set the height in storyboard to fit only one line.
  6. 将故事板中的高度设置为只适合一行。
  7. In your code after setting the text of UILabel call sizeToFit.
  8. 在您的代码中设置了UILabel的文本后调用sizeToFit。

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

这将使你的UILabel调整到你想要的最大宽度和高度的范围内,并且文本始终是顶部对齐的。

#3


12  

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

我用了nevan king的答案来回答我的问题:将文本垂直对齐到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/*/verticalAlignUILabel.zip

owolf.net/uploads/*/verticalAlignUILabel.zip

#4


6  

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.

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

#5


1  

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 .

我用它来做垂直对齐。

#6


1  

There is an easy solution for cases where the height of a label doesn't need to be constant: put your Label in 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:

对于标签的高度不需要是常量的情况,有一个简单的解决方案:将标签放在堆栈视图中。一定要在堆栈视图中添加引导和尾随常量。下面是如何在故事板中实现的截图:

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

#7


0  

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 UILabel and make sure it is being set to >= 0 as shown in the image below.

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

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