Vertically align UILabel

前端 未结 10 1534
故里飘歌
故里飘歌 2020-12-31 10:34

I am trying to vertically align the text in the UILabel view of my app. The problem is that I want the text to be vertically aligned to the top and the size of the label to

相关标签:
10条回答
  • 2020-12-31 10:55

    I did it what you want by implementing a category:

    .h

    @interface UILabel (VerticalAlign)
    - (void)alignTop;
    - (void)alignBottom;
    @end
    

    .m

    @implementation UILabel (VerticalAlign)
    
    #pragma mark
    #pragma mark - Align Methods
    
    - (void)alignTop
    {
        [self setFrame:[self newLabelFrame:UIControlContentVerticalAlignmentTop]];
    }
    
    - (void)alignBottom
    {
        [self setFrame:[self newLabelFrame:UIControlContentVerticalAlignmentBottom]];
    }
    
    #pragma mark
    #pragma mark - Helper Methods
    
    - (CGRect)newLabelFrame:(UIControlContentVerticalAlignment)alignment
    {
        CGRect labelRect = self.frame;
    
        NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading;
    
        CGRect textRect = [self.text boundingRectWithSize:CGSizeMake(227.f, CGFLOAT_MAX)
                                                  options:options
                                               attributes:@{NSFontAttributeName:self.font}
                                                  context:nil];
    
        CGFloat textOffset = labelRect.size.height - textRect.size.height;
    
        UIEdgeInsets contentInsets;
        if (alignment == UIControlContentVerticalAlignmentTop)
        {
            if (textOffset < (labelRect.size.height/2.f))
            {
                contentInsets = UIEdgeInsetsMake(-textOffset, 0.f, 0.f, 0.f);
            }
            else
            {
                contentInsets = UIEdgeInsetsMake(0.f, 0.f, textOffset, 0.f);
            }
        }
        else
        {
            if (textOffset < (labelRect.size.height/2.f))
            {
                contentInsets = UIEdgeInsetsMake(0.f, 0.f, -textOffset, 0.f);
            }
            else
            {
                contentInsets = UIEdgeInsetsMake(textOffset, 0.f, 0.f, 0.f);
            }
        }
    
        return UIEdgeInsetsInsetRect(labelRect, contentInsets);
    }
    
    @end
    

    Remember that you need to set "NumberOfLines" to 0. This example works with multilines options!

    After you implement above category, you can simply do:

    [myLabel setText:finalRecipe];
    [myLabel alignTop];
    

    Cheers!

    0 讨论(0)
  • 2020-12-31 10:55

    nice hack... my two cents for swift 4.2/5:

    self.optTextMessage.numberOfLines =  0
    self.optTitle.lineBreakMode  = .byWordWrapping
    

    and add \n.. as suggested..

    0 讨论(0)
  • 2020-12-31 10:57

    dont use UILabel for this. Use UIButton with UserInteractionEnabled = NO; and that has options to vertical or horizontal align text inside it.

    Here you go:

    Swift:

    btnDetail.titleLabel?.numberOfLines = 5
    btnDetail.titleLabel?.lineBreakMode = .byCharWrapping
    btnDetail.contentVerticalAlignment = .top
    btnDetail.contentHorizontalAlignment = .left
    btnDetail.isUserInteractionEnabled = false
    btnDetail.autoresizesSubviews = true
    btnDetail.autoresizingMask = .flexibleWidth
    

    Obj-C

    [btnDetail.titleLabel setNumberOfLines:5];
    [btnDetail.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];
    [btnDetail setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
    [btnDetail setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btnDetail setUserInteractionEnabled:NO];
    btnDetail.autoresizesSubviews = YES;
    btnDetail.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    0 讨论(0)
  • 2020-12-31 10:59

    In addition to the above recommending setting the number of lines to 0 and the line break to Word Wrap be sure to not specify a height constraint. New lines are automatically added as needed by the displayed text and the text is always vertically aligned to the top of the label.

    0 讨论(0)
提交回复
热议问题