我希望下面的标签(黄色)至少是两行而不是一行。
我确保Use Autolayout
在 Interface Builder 中取消选中。当我设置numberOfLines
从 0 到 2 时,我得到两个单词堆叠在一起,黄色背景与单词紧密贴合。无论lineBreakMode
是NSLineBreakByWordWrapping
还是,结果都是一样的NSLineBreakByTruncatingTail
。使用与否的结果设置术语Label的框架也是一样的,使用与否sizeWithAttributes
也是一样的。sizeToFit
我也尝试过将标签设为 aUILabel
而不是 的子类UILabel
,即TTTAttributedLabel
,但结果是一样的。
_termsLabel.font = [UIFont systemFontOfSize:12];
_termsLabel.textColor = [UIColor grayColor];
_termsLabel.textAlignment = NSTextAlignmentCenter;
_termsLabel.lineBreakMode = NSLineBreakByWordWrapping;
_termsLabel.numberOfLines = 0;
_termsLabel.delegate = self;
_termsLabel.backgroundColor = [UIColor yellowColor];
// Terms label
NSString *termsText = [NSString stringWithFormat:@"%@ %@ %@ %@", NSLocalizedString(@"TermsIAgree", nil),
NSLocalizedString(@"SettingsTOS", nil),
NSLocalizedString(@"LocalizedAnd", nil),
NSLocalizedString(@"SettingsPrivacyPolicy", nil)];
_termsLabel.text = termsText;
_termsLabel.linkAttributes = @{ (__bridge NSString *)kCTUnderlineStyleAttributeName : [NSNumber numberWithBool:YES]};
CGSize termsSize = [_termsLabel.text sizeWithAttributes: @{ NSFontAttributeName : _termsLabel.font}];
_termsLabel.frame = CGRectMake(65,
395,
termsSize.width, termsSize.height);
[_termsLabel addLinkToURL:[NSURL URLWithString:TOS_URL] withRange:[termsText rangeOfString:NSLocalizedString(@"SettingsTOS", nil)]];
[_termsLabel addLinkToURL:[NSURL URLWithString:PRIVACY_POLICY_URL] withRange:[termsText rangeOfString:NSLocalizedString(@"SettingsPrivacyPolicy", nil)]];
编辑CGSize termsSize = [_termsLabel.text sizeWithFont:_termsLabel.font forWidth:200 lineBreakMode:NSLineBreakByWordWrapping];
:通过使用Yet height
of the termsSize
is then查找术语文本大小14
,仅生成一行:
我怎样才能得到第二行?解决方案此时,只需添加[_termsLabel sizeToFit]
.