这与其他所有内容略有不同“我如何将问题置于UILabel
个问题中心......
我有UILabel
,其中包含一些文字,我希望将文字垂直居中于UILabel
。有什么大不了的,对吧?这是默认值。问题来了,因为我的文字是动态的,我打开了自动收缩。随着文本变大,字体大小缩小。你得到这种行为。
请注意,字体基线没有移动,我希望它移动,以便数字在UILabel's
框架中垂直居中。
简单,对吗?我只记得viewDidLoad
self.workoutTimeCenter = _workoutTimeLabel.center;
然后我在更改文本后调用sizeToFit
,对吗?
[_workoutTimeLabel sizeToFit];
_workoutTimeLabel.center = _workoutTimeCenter;
嗯,我想,sizeToFit
完全应该做的事情,调整框架的大小,使文字合适而不缩小!
如何在尊重基线和自动收缩的同时将文本垂直居中UILabel
? (注意, iOS5 以及后来的解决方案很好,我甚至可以处理 iOS6 及以后的解决方案。)
答案 0 :(得分:93)
根据我的经验,您只需将-[UILabel baselineAdjustment]
属性设置为UIBaselineAdjustmentAlignCenters
即可达到您所描述的效果。
来自docs:
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
答案 1 :(得分:2)
-(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];
注意:我从xib获取了UILabel。您也可以通过编程方式进行,在这种情况下,您必须设置其文本对齐方式NSTextAlignMentCenter
答案 2 :(得分:2)
答案 3 :(得分:1)
尝试实现此逻辑:
-(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;
}
它给了我很多帮助。希望它适合你。
答案 4 :(得分:-4)
尝试
yourLabel.textAlignment = UITextAlignmentCenter;