Sau*_*abh 64
您可以计算字符串出现的大小,然后将UILabel的框架设置为该大小,请参阅以下代码作为示例 -
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
Run Code Online (Sandbox Code Playgroud)
更新 -
使用sizeWithAttributes:相反,现在需要的NSDictionary.使用密钥UITextAttributeFont和您的字体对象传递对,如下所示:
CGSize size = [string sizeWithAttributes:
@{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}];
Run Code Online (Sandbox Code Playgroud)
在iOS 7中检查替换是否已弃用sizeWithFont:更多细节
noR*_*ema 16
这也将起到作用,并将考虑属性文本
label.attributedText = attrString;
CGSize maximumLabelSize = CGSizeMake(187,CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maximumLabelSize];
CGRect labelFrame = label.frame;
labelFrame.size.height = requiredSize.height;
label.frame = labelFrame;
Run Code Online (Sandbox Code Playgroud)
sch*_*her 10
'sizeWithFont:'已弃用:首先在iOS 7.0中弃用.
所以试试吧 sizeWithAttributes
- (void)viewDidLoad
{
[super viewDidLoad];
label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 300, 20)];
label.backgroundColor = [UIColor blueColor];
const CGFloat fontSize = 30;
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
UIColor *foregroundColor = [UIColor blackColor];
attrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName,
foregroundColor, NSForegroundColorAttributeName
, nil];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:@"Test Text"
attributes:attrs];
[label setAttributedText:attributedText];
[self.view addSubview:label];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGSize expectedLabelSize = [label.text sizeWithAttributes:attrs]; // iOS 7 Code <--
CGRect newFrame = label.frame;
newFrame.size.width = expectedLabelSize.width;
label.frame = newFrame;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:@"Longer Test Text"
attributes:attrs];
[label setAttributedText:attributedText];
}
Run Code Online (Sandbox Code Playgroud)
作为sizeWithFont:在iOS的7已经过时,你需要使用sizeWithAttributes(如maver解释这里).为了简化代码,您可以添加一个可以重用的方法,如下所示:
-(CGRect)rectForText:(NSString *)text
usingFont:(UIFont *)font
boundedBySize:(CGSize)maxSize
{
NSAttributedString *attrString =
[[NSAttributedString alloc] initWithString:text
attributes:@{ NSFontAttributeName:font}];
return [attrString boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
}
Run Code Online (Sandbox Code Playgroud)
并利用它
CGSize maximumLabelSize = CGSizeMake(280,9999);
UIFont *font = [UIFont systemFontOfSize:20];
CGRect titleRect = [self rectForText:post.title // <- your text here
usingFont:font
boundedBySize:maximumLabelSize];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49340 次 |
| 最近记录: |