ios 如何从底部对齐 UILabel 文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18247934/
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-31 00:44:47  来源:igfitidea点击:

How to align UILabel text from bottom?

iphoneiosalignmentuilabel

提问by NSUserDefault

How the UILabelcan be aligned from bottom. Let say, my label can hold three line of text.If the input text is single line, then this line should come bottom of the label.Please refer the below image for better understanding. The orange area is the full frame of label.Currently it has one line and it is aligned center. So what I want is, it should always aligned bottom regardless of how many lines.

如何UILabel从底部对齐。比方说,我的标签可以容纳三行文本。如果输入文本是单行,那么这行应该在标签的底部。请参考下图以便更好地理解。橙色区域是label的全框,目前只有一行,居中对齐。所以我想要的是,无论有多少行,它都应该始终与底部对齐。

enter image description here

在此处输入图片说明

Please suggest your ideas.

请提出你的想法。

Thank you.

谢谢你。

采纳答案by Paras Joshi

Here are two ways of doing that...

这里有两种方法可以做到这一点......

1.First set numberOfLinesto 0 and then use sizeToFitproperty of UILabelso your UILabeldisplay with its contentSize.

1.首先设置numberOfLines为 0,然后使用的sizeToFit属性UILabel使您的UILabel显示与它的contentSize.

yourLabel.numberOfLines = 0;

[yourLabel sizeToFit];

See more information from this link: Vertically align text within a UILabel

从此链接查看更多信息:垂直对齐 UILabel 中的文本

2.Another option is to take UITextFieldinstead of UILabeland set userInteractionEnabledto NOlike below...

2.另一种选择是UITextField代替UILabel并设置userInteractionEnabledNO如下所示......

[yourTextField setUserInteractionEnabled:NO];

and then set the contentVerticalAlignmentproperty to bottom like below...

然后将contentVerticalAlignment属性设置为底部,如下所示...

[yourTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];

UPDATE

更新

Also, with UITextField, we can't achieve multiple lines. So instead we can use UITextViewand set its userInteractionEnabledto NO. Then, use the code below to make it bottom aligned.

此外,使用UITextField,我们无法实现多行。因此,我们可以使用UITextView并将其设置userInteractionEnabledNO. 然后,使用下面的代码使其底部对齐。

CGFloat topCorrect = ([label bounds].size.height - [label contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
label.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};

回答by Dustin

Subclass UILabel

子类 UILabel

@interface Label : UILabel

@end

Then override drawTextInRect like so

然后像这样覆盖 drawTextInRect

@implementation Label

- (void)drawTextInRect:(CGRect)rect
{
    if(alignment == top) {

        rect.size.height = [self sizeThatFits:rect.size].height;
    }

    if(alignment == bottom) {

        CGFloat height = [self sizeThatFits:rect.size].height;

        rect.origin.y += rect.size.height - height;
        rect.size.height = height;
    }

    [super drawTextInRect:rect];
}

@end

回答by Dunbar

Swift 4.2 version using the contentModeproperty to set top and bottom:

Swift 4.2 版本使用contentMode属性设置顶部和底部:

class VerticalAlignedLabel: UILabel {

    override func drawText(in rect: CGRect) {
        var newRect = rect
        switch contentMode {
        case .top:
            newRect.size.height = sizeThatFits(rect.size).height
        case .bottom:
            let height = sizeThatFits(rect.size).height
            newRect.origin.y += rect.size.height - height
            newRect.size.height = height
        default:
            ()
        }

        super.drawText(in: newRect)
    }
}

Then setup your label like that:

然后像这样设置你的标签:

let label: VerticalAlignedLabel = UILabel()
label.contentMode = .bottom

回答by Tobe

i only set a bottom constraint to the super view in IB which works for me without using code and also number of Lines for a maximum constraint.

我只为 IB 中的超级视图设置了一个底部约束,这对我有用,而无需使用代码以及最大约束的行数。

回答by Shruthi Palchandar

In IB as @Tobe said Bottom constraint to superview should work, Incase if you have multiple subview or horizontal stack view with one element to have bottom constraint then use Layout Margin to be Fixed with bottom less than other margins enter image description here

在 IB 中,@Tobe 说对超级视图的底部约束应该有效,如果您有多个子视图或水平堆栈视图,其中一个元素具有底部约束,则使用布局边距固定,底部小于其他边距 在此处输入图片说明

回答by barrast

I had the same issue. Here is how I made to align the text to the bottom of my UILabel:

我遇到过同样的问题。这是我如何将文本与 UILabel 的底部对齐:

- (void)alignLabel:(UILabel *)l withText:(NSString *)text verticalAlignOption:(int)vertAlign{
CGSize stringSize = [text sizeWithFont:l.font constrainedToSize:l.frame.size lineBreakMode:l.lineBreakMode];

switch (vertAlign) {
    case 0: // align = top
        l.frame = CGRectMake(l.frame.origin.x,
                                   l.frame.origin.y,
                                   l.frame.size.width,
                                   stringSize.height
                                   );
        break;
    case 1: // align = bottom
        l.frame = CGRectMake(l.frame.origin.x,
                                   (l.frame.origin.y + l.frame.size.height) - stringSize.height,
                                   l.frame.size.width,
                                   stringSize.height
                                   );
        break;
    case 2: // align = middle
        // default
        break;
}

l.text = text;
}

Ahd you simple call the method like this to align to the bottom:

啊,你简单地调用这样的方法来对齐到底部:

[self alignLabel:self.mediaTitle withText:@"My text to align" verticalAlignOption:1];

回答by autremoi

Another option: use one label for your background color, I call this one originalLabel, and another for the text, called textLabel in my example. Then calculate the height and Y coordinate for textLabel:

另一种选择:使用一个标签作为背景颜色,我称之为 originalLabel,另一个用于文本,在我的示例中称为 textLabel。然后计算 textLabel 的高度和 Y 坐标:

[textLabel sizeToFit];
int height = textLabel.frame.size.height;
int yCoord = originalLabel.frame.origin.y + 
          originalLabel.frame.size.height - height;
textLabel.frame = CGRectMake( originalLabel.frame.origin.x, yCoord,
          textLabel.frame.size.width, height);

回答by ahmedalkaff

You can subclass UILabeland overriding the method :

您可以子类化UILabel和覆盖该方法:

- (void)drawTextInRect:(CGRect)rect

call super drawTextInRectwith the rect where you want to use.

drawTextInRect使用您要使用的矩形调用 super 。