简体   繁体   中英

UILabel not drawing multiline

I want the label below (in yellow) to be at least two lines rather one.

问题

I've made sure to uncheck Use Autolayout in Interface Builder. When I set the numberOfLines from 0 to 2, I get two words stacked on top of each other, with the yellow background tightly fitting the words. The result is the same regardless of whether the lineBreakMode is NSLineBreakByWordWrapping or NSLineBreakByTruncatingTail . It's also the same if I set the frame of the terms Label using the result of sizeWithAttributes or not, and it's the same if I use sizeToFit or not. I've also tried making the label a UILabel rather than a subclass of UILabel , which is TTTAttributedLabel , but the result is the same.

_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)]];

EDIT : By finding the terms text size using CGSize termsSize = [_termsLabel.text sizeWithFont:_termsLabel.font forWidth:200 lineBreakMode:NSLineBreakByWordWrapping]; Yet the height of the termsSize is then 14 , resulting in just one line:

在此输入图像描述

How can I get the second line? SOLUTION At this point, just add [_termsLabel sizeToFit] .

If you've got static text, just set the break mode to wrap, set lines to the number you want, and adjust the label's frame in interface builder until it wraps the way you like. Of you've got dynamic text, you can use sizeToFit after setting the label's text to have it automatically adjust it's height to fit the specified width:

  1. Set frame to max desired width
  2. Set lines to 0
  3. Set break mode to wrap
  4. Call sizeToFit

确定标签的最大宽度,并尝试使用sizeWithFont:forWidth:lineBreakMode:具有此值的方法和所需的NSLineBreakMode来获取结果字符串的边界框的大小。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM