使用自动收缩在UILabel中垂直居中

这与其他所有“我如何将文本置于UILabel ”的问题稍有不同......

我有一个带有一些文本的UILabel ,我想在UILabel垂直居中文本。 有什么大不了的,对吧? 这是默认设置。 问题出现是因为我的文本是动态的,而且我打开了自动打开功能。 随着文字变大,字号缩小。 你会得到这种行为。

在这里输入图像描述

请注意,字体基线没有移动,我希望它移动,以便数字垂直居中在UILabel's框架中。

很简单,对吧? 我只记得viewDidLoad的框架原始中心

    self.workoutTimeCenter = _workoutTimeLabel.center;

然后我在更改文本后调用sizeToFit ,对吧?

    [_workoutTimeLabel sizeToFit];
    _workoutTimeLabel.center = _workoutTimeCenter;

那么, sizeToFit ,我想,确实是它应该做的,调整框架的大小,使文本适合不缩小!

在这里输入图像描述

如何在尊重基线和自动收缩的同时将文本垂直居中放置在UILabel ? (请注意, iOS5及更高版本的解决方案很好,我甚至可以处理iOS6及更高版本的解决方案。)


根据我的经验,您可以将-[UILabel baselineAdjustment]属性设置为UIBaselineAdjustmentAlignCenters以实现您描述的效果。

从文档:

baselineAdjustment

控制文本需要缩小以适应标签时文本基线的调整方式。

@property(nonatomic) UIBaselineAdjustment baselineAdjustment

讨论

如果adjustsFontSizeToFitWidth属性设置为YES ,则此属性控制在需要调整字体大小的情况下文本基线的行为。 此属性的默认值是UIBaselineAdjustment AlignBaselines 。 该属性仅在numberOfLines属性设置为1时有效。

UIBaselineAdjustmentAlignCenters
根据相对于其边界框的中心调整文本。

编辑:添加一个完整的视图控制器,演示了这一点:

@interface TSViewController : UIViewController
@end

@implementation TSViewController

- (void) addLabelWithFrame: (CGRect) f baselineAdjustment: (UIBaselineAdjustment) bla
{
    UILabel* label = [[UILabel alloc] initWithFrame: f];
    label.baselineAdjustment = bla;
    label.adjustsFontSizeToFitWidth = YES;
    label.font = [UIFont fontWithName: @"Courier" size: 200];
    label.text = @"00";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor lightGrayColor];
    label.userInteractionEnabled = YES;
    [self.view addSubview: label];

    UIView* centerline = [[UIView alloc] initWithFrame: CGRectMake(f.origin.x, f.origin.y+(f.size.height/2.0), f.size.width, 1)];
    centerline.backgroundColor = [UIColor redColor];
    [self.view addSubview: centerline];

    UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(onTap:)];
    [label addGestureRecognizer: tgr];
}

- (void) viewDidLoad
{
    [super viewDidLoad];

    [self addLabelWithFrame: CGRectMake(0, 0, 320, 200)
         baselineAdjustment: UIBaselineAdjustmentAlignCenters];

    [self addLabelWithFrame: CGRectMake(0, 220, 320, 200)
         baselineAdjustment: UIBaselineAdjustmentAlignBaselines];
}

- (void) onTap: (UITapGestureRecognizer*) tgr
{
    UILabel* label = (UILabel*)tgr.view;
    NSString* t = [label.text stringByAppendingString: @":00"];
    label.text = t;
}

@end

-(void)fitVerticallyToLabel:(UILabel *)lbl
{
    CGFloat fontSize = lbl.frame.size.width / lbl.text.length;
    [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];

    CGRect rect = lbl.frame;
    rect.origin.y += rect.size.height - fontSize;
    rect.size.height = fontSize;
    [lbl setFrame:rect];
}

如何使用:将文本设置为标签后调用此方法。

    label.text = @"text";
    [self fitVerticallyToLabel:label];

在这里输入图像描述在这里输入图像描述

注意:我ahev从xib拿到了UILabel。 你也可以以编程方式使用它,在这种情况下,你将不得不设置其文本对齐NSTextAlignMentCenter


尝试实现这个逻辑:

-(void)adjustLabel1Text1:(NSString *)text1 
{
    UILabel *lbl_first = [UIFont fontWithName:@"Helvetica" size:12];



    text1 = [text1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    float hedingLblHeight = [self calculateHeightOfTextFromWidth:text1 : [UIFont fontWithName:@"Helvetica" size:12] :118 :UILineBreakModeWordWrap];

    lbl_first.text=text1;


    [lbl_first setFrame:CGRectMake(lbl_first.frame.origin.x, lbl_first.frame.origin.y, 118, hedingLblHeight)];
    lbl_first.lineBreakMode = UILineBreakModeWordWrap;
    lbl_first.numberOfLines = 0;
    [lbl_first sizeToFit];




//////////Adjust the lable or any UIControl below this label accordingly.

    float endResultHeight=[self calculateHeightOfTextFromWidth:text2 : [UIFont fontWithName:@"Helvetica" size:15] :299 :UILineBreakModeWordWrap];

    if(hedingLblHeight>secondImgTitleHeight)
    {
    [lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
    }
    else
    {
        [lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];

    }

    lbl_endResult.lineBreakMode=UILineBreakModeWordWrap;
    lbl_endResult.numberOfLines = 0;
    [lbl_endResult sizeToFit];



}

-(float) calculateHeightOfTextFromWidth:(NSString*)text : (UIFont*) withFont:(float)width :(UILineBreakMode)lineBreakMode
{

    CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

    return suggestedSize.height;
}

它帮助了我很多,希望它对你有用。

链接地址: http://www.djcxy.com/p/28293.html

上一篇: Center vertically in UILabel with autoshrink

下一篇: Vertically align text in a UILabel