根据font的大小 和string来 动态 获取label的长度

来源:互联网 发布:java怎么调用数组 编辑:程序博客网 时间:2024/06/07 04:51

CGSize wordSize = [@“要判断的字符串” sizeWithFont:allBtn.titleLabel.font];


根据数据动态添加label

 if (self.originNamePrams) {

                                                  

   CGFloat originX = 20;

                                                    

 for (int i = 0; i < self.originNamePrams.count; i++) {

                                                       

  UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];

                                                         tempBtn.tag = i;

                                                         tempBtn.titleLabel.font = tempFont;

                                                         tempBtn.backgroundColor = [UIColor clearColor];

                                                         

CGSize wordSize = [[[self.originNamePrams objectAtIndex:i] pramValue] sizeWithFont:tempBtn.titleLabel.font];

                                                        

 tempBtn.frame = CGRectMake(originX, 5, wordSize.width > 25 ? wordSize.width + 20 : 45, 25);

                                                        

 [tempBtn setTitle:[[self.originNamePrams objectAtIndex:i] pramValue] forState:UIControlStateNormal];

                                                       

  [tempBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

                                                        

 [tempBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

                                                         

[tempBtn setBackgroundImage:[UIImage imageNamed:@"vod_screen_btn_selected"] forState:UIControlStateSelected];

                                                        

 [tempBtn addTarget:self action:@selector(whenSortOriginNameBtnTapped:) forControlEvents:UIControlEventTouchUpInside];

                                                        

 [self.sortOriginScrollView addSubview:tempBtn];

                                                         

             

 originX += tempBtn.bounds.size.width;

                                                         

                   

 if (i == 0) {

                             

   [tempBtn setSelected:YES];

                              

     }

                                  

  }