设置UILabel的属性文本并使用setFontSizeToFitWidth时,属性文本字体大小按预期调整大小。但是,当属性字符串字体大小调整为较小的字体大小时,文本不会在UILabel内部垂直对齐。垂直对齐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];
谁能帮助我实现这个目标,从而使文本也垂直对齐?
谢谢你们。
这可能帮助你http://stackoverflow.com/questions/1054558/vertically-align-text -within-a-uilabel – iOSDeveloper