ios动态调整大小标签

Seb*_*Seb 28 text font-size uilabel ios

我试图在网上搜索,但没有找到我的问题的明确答案所以我来征求你的专家意见.我有一个带有2个标签的视图.第一个标签用于显示名称的缩写,2-3个字母.第二个标签显示全名.

我有的问题是,是否有办法根据给定的字体类型,大小和字符串长度动态调整标签大小?我问,因为我希望第二个标签靠近第一个标签,两者之间没有太多的空白空间,或者没有第一个标签与第二个标签重叠.

这不是一个标签的原因是因为第一个标签应该有更大的字体和不同的颜色方案,然后是第二个标签.

任何意见是极大的赞赏.

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)


edu*_*udi 8

作为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)


Sla*_*cho 7

除了Saurabh的回答.我遇到了同样的问题,你应该添加这一行
[yourLabel setNumberOfLines:0];
,以便整行文字以X行显示.