【问题标题】:How to set UILable/UIButton(custom) Size after adding string?添加字符串后如何设置 UILabel/UIButton(custom) Size?
【发布时间】:2011-05-30 18:57:38
【问题描述】:

我必须在带有标题的滚动视图中添加标签/自定义按钮的数量。

所以我得到每个字符串的长度并乘以它的字体大小并得到整个字符串的宽度,但我没有得到每个标签的确切值。

如果有小伙伴有想法告诉我。

谢谢

Ankit

【问题讨论】:

    标签: iphone objective-c xcode ios4


    【解决方案1】:

    做一些类似dis...!!!

    NSString * titleString=pod.title;
        CGSize constraint = CGSizeMake(200,999);
        CGSize size = [titleString sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
        titleLabel=[[[UILabel alloc] initWithFrame:CGRectMake(120, 24, 200, size.height)] autorelease];
        [titleLabel setText:titleString];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setNumberOfLines:0];
        titleLabel.lineBreakMode=UILineBreakModeWordWrap;
        [titleLabel setFont:[UIFont systemFontOfSize:14]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setTextAlignment:UITextAlignmentCenter];
        [scrollView addSubview:titleLabel];
    

    【讨论】:

    • 感谢朋友们抽出宝贵时间...嘿 suny,我认为这也兼容 UIButton(cus.)。
    【解决方案2】:

    有一个很好的方法sizeWithFont:。你可以查一下here

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多