2014-01-30 79 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]; 
相關問題