以编程方式创建的UILabel宽度

3
我正在尝试通过编程方式创建一个适应其内容的UILabel。我所能做的最好的方法是使用NSStringsizeWithAtributes方法获取其内容所需的宽度,然后应用一个将宽度固定为该值的约束。
这是使用自动布局应该采取的方式吗?

1
只是提醒一下,不要忘记使用“sizeToFit”。 - Fattie
4个回答

3

首先阅读有关 任何与UILabel相关的问题的最佳答案

此外,通过使用以下代码,您可以创建动态大小的UILabel

UILabel *myLabelName = [[UILabel alloc] init];
myLabelName.backgroundColor = [UIColor redColor];
[myLabelName  setFont: [UIFont fontWithName:@"OpenSans" size:13]]; // set font and it's size as per your requirement.
myLabelName.textAlignment = NSTextAlignmentLeft;
myLabelName.frame = CGRectMake(lblComplainTitle.frame.origin.x + lblComplainTitle.frame.size.width + 7, lblComplainTitle.frame.origin.y +2, 160, 20);
myLabelName.text = @"This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text.";
myLabelName.textColor = [UIColor blackColor];
[self setDynamicHeightOfLabel:myLabelName withLblWidth:160 andFontSize:13]; // here in this function you need to pass object of label with width (as you wabt) and font size
[self.view addSubview:myLabelName];

函数代码,名称为setDynamicHeightOfLabel:withLblWidth:andFontSize

-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
    CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
    CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
    CGRect lblFrame = myLabel.frame;
    lblFrame.size.height = expecteingmyLabelSize.height;
    myLabel.frame = lblFrame;
    int addressLine = myLabel.frame.size.height/fontSize;
    myLabel.numberOfLines = addressLine;
}

我创建了函数,因为你可以创建任何大小的动态标签而不需要重复编写代码。


3

如果你需要关于自动布局方面的帮助,我尝试解释一下:

    UILabel *testLabel = [UILabel new];
    [testLabel setFont:[UIFont systemFontOfSize:16]];
    [testLabel setText:@"Test text for size context"];
    [testLabel setTextAlignment:NSTextAlignmentLeft];
    [testLabel setNumberOfLines:0]; //0 - infiniy lines if not enough space more
    [testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; // Autolayout rule: The higher the priority, the more important hold the text.. by horizontal
    [testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//some like above but verticale
    [testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];// reverse like Hugging but Compression. then lower priority then text croping.
    [testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
    [testLabel preferredMaxLayoutWidth:200];//Start new line each 200 points width
    [testLabel setPreferredMaxLayoutWidth:YES];// resize Font size by label size.
    [testLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; // Disable autolaresize, enable autolayouts

2

使用 autolayout 实现此目标需要执行几个步骤。

  1. 为标签设置布局约束。
  2. 设置低优先级的 height 约束。
  3. numberOfLines 设置为 0 以允许多行文本。
  4. 为标签设置 preferredMaxLayoutWidth

preferredMaxLayoutWidth 属性用于计算标签的高度。

此属性影响标签应用布局约束时的大小。在布局过程中,如果文本超出此属性指定的宽度,则额外的文本会被换行到一个或多个新行中,从而增加标签的高度。


1

你可以通过编程的方式来管理标签的宽度:

CGRect frame;

frame =self.yourLabel.frame;
frame.size.width +=20;
self.yourLabel.frame=frame;

谢谢


1
这个问题是关于自动布局的。在自动布局中,你不使用矩形。 - Marius Waldal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接