iOS 动态设置标签

来源:互联网 发布:unity3d t4m插件 编辑:程序博客网 时间:2024/05/19 15:22





@property (strong,nonatomicUIView *tableHeadView;



    NSMutableArray *hotSearchArr;

hotSearchArr = [[NSMutableArray alloc] initWithObjects:@"ceshi1",@"ceshi2ceshi2",@"ceshi3ceshi3ceshi3ceshi3ceshi3ceshi",@"ceshi4",@"ceshi5",@"ceshi6",@"ceshi7",@"ceshi8ceshi8ceshi8ceshi8", nil];


// 屏幕高度

#define SCREEN_HEIGHT           [[UIScreen mainScreen] bounds].size.height

// 屏幕宽度

#define SCREEN_WIDTH            [[UIScreen mainScreen] bounds].size.width


- (UIView *)tableHeadView{

    

    [hotSearchArr addObject:@"hotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArr"];

    [hotSearchArr addObject:@"hotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArr"];

    [hotSearchArr addObject:@"hotSearchAr"];

    [hotSearchArr addObject:@"hotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArrhotSearchArr"];

    [hotSearchArr addObject:@"hotSearchAr"];


    if (_tableHeadView) {

       for (UIView *subView in_tableHeadView.subviews) {

            [subView removeFromSuperview];

        }

    }


        _tableHeadView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,120)];

        _tableHeadView.backgroundColor = [UIColorwhiteColor];

    

        UILabel *hotSearchLab = [[UILabelalloc]initWithFrame:CGRectMake(20,0,100,40)];

        hotSearchLab.text =MYLocalizedString(@"remensousuo",@"");

        hotSearchLab.textColor =SearchViewDeepColor;

        hotSearchLab.font =SearchViewTextFont;

        hotSearchLab.textAlignment =NSTextAlignmentLeft;

        [_tableHeadViewaddSubview:hotSearchLab];

    

        CGFloat rowSpace =20.0;

        CGFloat colSpace =15.0;


        CGFloat hotTotalWidth =20.0f;

        CGFloat hotTotalHeight =0.0f + (hotSearchLab.frame.size.height);

        CGFloat hotTextHeight =0.0f;

        

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

                

            NSDictionary *attribute =@{NSFontAttributeName:SearchViewTextFont};

            CGSize size = [hotSearchArr[i]boundingRectWithSize:CGSizeMake(1000,300)

                                                    options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading

                                                  attributes:attributecontext:nil].size;


            UIButton *hotSearchBtn = [[UIButtonalloc]init];


            if (size.width >=SCREEN_WIDTH -2 * rowSpace) {

                

                hotSearchBtn.frame =CGRectMake(rowSpace, hotTotalHeight -3*colSpace,SCREEN_WIDTH - (2 * rowSpace), size.height + colSpace);

            }else{

                hotSearchBtn.frame =CGRectMake(hotTotalWidth, hotTotalHeight, size.width + rowSpace, size.height + colSpace);

            }

            [hotSearchBtn setTitle:hotSearchArr[i]forState:UIControlStateNormal];

            [hotSearchBtn setTitleColor:SearchViewPoolColorforState:UIControlStateNormal];

            hotSearchBtn.titleLabel.font =SearchViewTextFont;

            hotSearchBtn.layer.borderWidth = 0.5f;

            hotSearchBtn.layer.borderColor = [UIColorlightGrayColor].CGColor;

            [hotSearchBtn addTarget:selfaction:@selector(hotSearchBtnAction:)forControlEvents:UIControlEventTouchUpInside];

            hotSearchBtn.tag = i;

        

            hotTotalWidth += hotSearchBtn.frame.size.width + rowSpace;

            hotTextHeight = hotSearchBtn.frame.size.height;

            

            if (hotTotalWidth >=_tableHeadView.frame.size.width) {

                hotTotalHeight += hotSearchBtn.frame.size.height + colSpace;

                hotTotalWidth = 20.0f;

                

                hotSearchBtn.frame =CGRectMake(hotTotalWidth, hotTotalHeight, hotSearchBtn.frame.size.width, hotSearchBtn.frame.size.height);

                hotTotalWidth += hotSearchBtn.frame.size.width + rowSpace;

            }

             [_tableHeadViewaddSubview:hotSearchBtn];

        }

    

        hotTotalHeight += hotTextHeight;

    

        _tableHeadView.frame =CGRectMake(0,0,SCREEN_WIDTH, hotTotalHeight + colSpace);

            

        UIView *lineView = [[UIViewalloc]initWithFrame:CGRectMake(15, hotTotalHeight + colSpace-1,SCREEN_WIDTH-15,0.5)];

        lineView.backgroundColor =COLOR(236,236,236,1);

        [_tableHeadViewaddSubview:lineView];

        

    return_tableHeadView;

}



原创粉丝点击