封装自定义View,创建N个按钮,按钮根据title长度Size-fit显示

为了实现  创建N多个按钮, 并且让按钮根据所给的title长度自适应, 并且高度自适应,封装了这个类


效果如图:



ButtonView.h


//
//  ButtonView.h
//  AiComic
//
//  Created by Damon on 15/2/26.
//  Copyright (c) 2015年 Damon. All rights reserved.
//

#import "BaseView.h"

@protocol searchButtonViewDelegate <NSObject>
// 协议方法:
//点击每个按钮时的协议方法
- (void)searchButtonViewClicked:(NSString *)str;
// 传出此时布局后View的高度
- (void)searchButtonViewFrame:(CGFloat)y;

@end

@interface ButtonView : BaseView
// 代理
@property (nonatomic, retain) id<searchButtonViewDelegate>delegate;
// 数组, 存储title
@property (nonatomic, retain) NSMutableArray *arr;
// View的高度
@property (nonatomic, assign) CGFloat y;
// 重写初始化方法
- (id)initWithFrame:(CGRect)frame buttonNumber:(NSInteger)number;
// 给数组赋值
- (void)setMyArr:(NSMutableArray *)arr;

@end

ButtonView.m文件


//
//  ButtonView.m
//  AiComic
//
//  Created by Damon on 15/2/26.
//  Copyright (c) 2015年 Damon. All rights reserved.
//

#import "ButtonView.h"

@interface ButtonView ()
// 按钮的数量
@property (nonatomic, assign) NSInteger number;
// 字体大小
@property (nonatomic,retain)UIFont *font;
@end

@implementation ButtonView

// 初始化
- (id)initWithFrame:(CGRect)frame buttonNumber:(NSInteger)number
{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.arr = [NSMutableArray array];
        self.number = number;
        for (int i = 1; i <= number; i++) {
            
            [self.arr addObject:[NSString stringWithFormat:@"%d",i]];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i;
            //  self.button0.backgroundColor = [UIColor redColor];
            [self addSubview:button];
            self.font = button.titleLabel.font;
            
        }
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.frame.size.width;
    CGFloat frontWidth = 10;
    CGFloat widthOfButton = 20;
    CGFloat buttonHeight = 20;
    self.y = 20;
    
    // 布局
    for (int i = 1; i <=self.arr.count; i++) {
        
        if (i == 1) {
            
            UIButton *button = (UIButton *)[self viewWithTag:i];
            button.frame = CGRectMake(frontWidth, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
            
        } else {
            
            UIButton *upButton = (UIButton *)[self viewWithTag:i - 1];
            
            UIButton *button = (UIButton *)[self viewWithTag:i];
            
            
            if ((upButton.frame.size.width + upButton.frame.origin.x + widthOfButton + frontWidth + [self getLabelSize:[self.arr objectAtIndex:i - 1]].width) > width) {
                self.y = self.y + 40;
                button.frame = CGRectMake(frontWidth, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
            } else {
                
                button.frame = CGRectMake(upButton.frame.origin.x + upButton.frame.size.width + widthOfButton, self.y, [self getLabelSize:[self.arr objectAtIndex:i - 1]].width, buttonHeight);
            }
            
        }
        
    }
    // 布局后  把当前高度传给代理
    [self.delegate searchButtonViewFrame:self.y];
    
}



// 获取字符串的宽度
- (CGSize)getLabelSize:(NSString *)str
{
    CGSize size = CGSizeMake(150, 20);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, nil];
    
    CGSize Labelsize = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return Labelsize;
}

// //点击每个按钮时的协议方法的实现
- (void)buttonClicked:(UIButton *)button
{
    // 让代理执行方法
    [self.delegate searchButtonViewClicked:button.titleLabel.text];
    
}
// 给标题数组赋值
- (void)setMyArr:(NSMutableArray *)arr
{
    //self.number = arr.count;
    [self.arr removeAllObjects];
    [self.arr addObjectsFromArray:arr];
    [self setTitleOfButton];
}

// 给每个button赋值
- (void)setTitleOfButton
{
    NSInteger i = 1;
    for (NSString *str in self.arr) {
        
        UIButton *button = (UIButton *)[self viewWithTag:i];
        
        [button setTitle:str forState:UIControlStateNormal];
        i++;
    }
    
    [self layoutSubviews];
}





/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end<span style="color:#ff0000;">
</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值