【发布时间】:2014-05-25 12:45:05
【问题描述】:
当设置 UILabel 的属性文本并使用 setFontSizeToFitWidth 时,属性文本字体大小会按预期调整。但是.. 当属性字符串字体大小调整为较小的字体大小时,文本在 UILabel 内没有垂直对齐。
我需要使用方法 adjustFontSizeToFitWidth 因为属性字符串的大小是可变的。我将最小字体大小设置为“15.0”,最大设置为“28.0”。因此我使用“15.0/28.0”的 minimumScaleFactor
我的代码:
NSAttributedString *balance = [[NSAttributedString alloc] initWithString:@"12390765298374652938756" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
// Create UILabel with a 10.0 point padding around the UILabel within the parent Rect
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 10, self.bounds.origin.y + 10, self.bounds.size.width - 20, self.bounds.size.height - 20)];
textLabel.attributedText = currencyWithBalance;
textLabel.font = [UIFont fontWithName:@"TitilliumWeb-Light" size:28.0];
textLabel.minimumScaleFactor = 15.0/28.0;
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 1;
textLabel.backgroundColor = [UIColor redColor];
[self addSubview:textLabel];
谁能帮我实现这一点,让文本也垂直对齐?
谢谢大家。
【问题讨论】:
标签: ios objective-c uilabel vertical-alignment nsattributedstring