我以前使用过 minimumFontSize
,但该功能现在已弃用,我不太了解 minimumScaleFactor
的工作原理。
我希望最大字体大小为 10,最小为 7。
如何使用比例因子将字体大小调整为 7?
UILabel
创建:
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
label.text = [labelName uppercaseString];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:9.5];
label.backgroundColor = [UIColor clearColor];
label.minimumScaleFactor = .1f;
[label addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label(WIDTH)]"
options:0
metrics:@{@"WIDTH" : [NSNumber numberWithFloat:buttonSize.width]}
views:NSDictionaryOfVariableBindings(label)]];
[contentView addSubview:label];
最佳答案
你需要设置label.adjustsFontSizeToFitWidth = YES;
https://stackoverflow.com/questions/14650990/