ios在运行时目标c更改标签宽度

时间:2016-03-14 07:51:17

标签: ios objective-c iphone uicollectionview

我制作了一个包含标签的集合视图,标签的数量从1到3是随机的。

如何更改此标签的宽度,如果标签显示为1,则使其宽度覆盖手机。如果拖曳标签使它们成为手机尺寸的一半和一半,如果是三个则相同?

1 个答案:

答案 0 :(得分:1)

因为我理解你的问题,你想要根据适合屏幕的随机数增长标签的大小。我只是动态地试了一下。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.



    //getting random number between 1 to 3
    int randomNumber =[self generateRandomNumberBetweenMin:1 Max:3];

    //Finding height of each label
    CGFloat widthOfLabel=self.view.frame.size.width/randomNumber;

    //create number's of label based on random number
    for (int i=0; i<randomNumber; i++) {

        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(i*widthOfLabel, 20, widthOfLabel, 20 )];
        label.text=[NSString stringWithFormat:@"%d",i];
        [label setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:label];
    }


}

//generate random numbers within range
-(int)generateRandomNumberBetweenMin:(int)min Max:(int)max
{
    return ( (arc4random() % (max-min+1)) + min );
}