【问题标题】:Dynamic placing of labels and buttons in UIView在 UIView 中动态放置标签和按钮
【发布时间】:2011-12-08 15:58:59
【问题描述】:

我想知道是否有填充 UIView 的动态方式。

例如,考虑我有几个 UILabel 对齐为行,其文本再次是动态的。

如果第一个UILabel 的文本有多行,我知道我可以调用

[myLabel sizeToFit];

根据内容调整自身大小。

是否有一种简单的方法可以将此信息传播到 UIView 的所有子视图(在我的情况下为 UILabel)并制作它们并相应地重新调整它们的空间。

我希望我足够清楚。

【问题讨论】:

    标签: iphone uiview uilabel


    【解决方案1】:

    对于应该作为行对齐的两个标签,您可以使用CGRectGetMaxY 函数。例如:

    UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f,20.0f, 100.0f, 200.0f)];
    [first setText:@"Text to display"];
    [first sizeToFit];
    [self.view addSubview:first];
    [first release];
    
    UILabel *second = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(first.frame), 100.0f, 200.0f];
    [second setText:@"Text to display"];
    [second sizeToFit];
    [self.view addSubview:second];
    [second release];
    
    UILabel *third = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(second.frame), 100.0f, 200.0f];
    [third setText:@"Text to display"];
    etc...
    

    您还可以根据其文本参数知道标签的预期大小:

    NSString *text = @"Text to display";
    CGSize size = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:10.0f] constrainedToSize:CGSizeMake(100.0f, FLT_MAX) lineBreakMode:UILineBreakModeTailTruncation];
    UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, size.height)];
    [first setText:text];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2023-03-24
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多