2014-01-30 77 views
3

我使用循环创建动态UIView。但我不能创造UIView根据的UILabel大小好心帮我,我可以做进一步开展..需要你们的帮助。我会如何根据UILabel内容调整/设置UIView大小

for(NSString *item in myArray) 
{ 
    length = item.length; 
    self.custom=[[CustomView alloc]init]; 
    self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(40, yAxis, 100+item.length, 44)]; 
    [self.custom setFrame:self.custom.Label.frame]; 

    [self.custom sizeToFit]; 

    yAxis=yAxis+50; 

    self.custom.tag=200+a; 
    a++; 

    [newViews addObject:self.custom]; 
    length = item.length; 

    self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5,length+20,40)]; 

    self.custom.button=[[UIButton alloc]initWithFrame:CGRectMake(85,10,12,10)]; 

    [self.custom.Label setText:item]; 

    // Tell the label to use an unlimited number of lines 
    [self.custom.Label setNumberOfLines:1]; 
    [self.custom.Label sizeToFit]; 
    self.custom.Label.textAlignment = UITextAlignmentCenter; 

    ![this is image i have created uiview according the array values where uilabel more width than uiview so i need to set uiview according the uilabel][1] 

    UIImage *btnImage = [UIImage imageNamed:@"button_droparrow.png"]; 
    [self.custom.button setImage:btnImage forState:UIControlStateNormal]; 
    [self.custom.button addTarget:self 
          action:@selector(buttonPressed:)forControlEvents:UIControlEventTouchDown]; 

    self.custom.button.tag=self.custom.button.tag+a; 
    self.custom.backgroundColor=[UIColor greenColor]; 
    custom.Label.text=item; 
    custom.Label.backgroundColor = [UIColor yellowColor]; 
    [self.custom addSubview:self.custom.button]; 
    [self.custom addSubview:custom.Label]; 
    // [self.custom addSubview:cv]; 

    [self.view addSubview:self.custom]; 
} 

回答

0

可以开始使用基于UILabel内容的大小欣赏以下码。

NSString * countString = aLbl.text; 
CGSize s = [countString sizeWithFont:[UIFont fontWithName:@"Helvetica-Light" size:12] constrainedToSize:CGSizeMake(100, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; 

而且使用下面的代码

CGRect rect = aView.frame; 
rect.size.width = s.width; 
rect.size.height = s.height; 
aView.frame = rect; 
+0

我试过,但不起作用 –

+0

转贴你出把图像 –

+0

我的得分低于10,所以我可以链接图像 –

0

试穿尺寸,以适合标签或视图

[yourlabel sizeToFit]; 

[yourview sizeToFit]; 
+0

亲爱的这是行不通的...任何其他解决方案? –

-1

变化setNumberOfLines改变的任何视图大小: 0并运行你的应用程序。我希望它能帮助你。我向你请求,如果这是帮助你,请给我投票。

[self.custom.Label setNumberOfLines:0]; 

self.custom.Label.text = @"xyz................."; 
[self.custom.Label sizeToFit]; 
相关问题