下边是.h和.m文件,需要源码联系本人加QQ1149363936
//HUCategoryView.h
//SuiYangPartyBuilding
//
//Created by李金梁on 2017/7/7.
//Copyright © 2017年unicom. All rights reserved.
//
#import
@interfaceHUCategoryView :UIView
/**
定义一个block
*/
typedefvoid(^EndEidtBlock)();
@property(nonatomic,strong)NSMutableArray*categoryArray;
@property(nonatomic,assign)floatcategoryHegith;
/**
用定义的block来声明一个变量
*/
@property(nonatomic,copy)EndEidtBlockendEidtBlock;
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray*)titleArray;
@end
//
//HUCategoryView.m
//SuiYangPartyBuilding
//
//Created by李金梁on 2017/7/7.
//Copyright © 2017年unicom. All rights reserved.
//
#import"HUCategoryView.h"
staticNSString*constfunctionCellId =@"functionCellId";
staticconstCGFloatkWidthOfButtonMargin =5;
staticconstCGFloatkFontSizeOfTabButton =12.0f;
#define BUTTONTAG100
#define tabItemNormalColorRGBA(3,3,3,1)
#define tabItemSelectedColorRGBA(3,3,3,1)
@implementationHUCategoryView
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray*)titleArray{
self= [superinitWithFrame:frame];
if(self) {
UILabel*titleLabel = [[UILabelalloc]initWithFrame:CGRectMake(10,0,ViewWidth,40)];
titleLabel.text=@"日志类型";
titleLabel.font= [UIFontsystemFontOfSize:17];
titleLabel.textAlignment=NSTextAlignmentLeft;
titleLabel.backgroundColor= [UIColorclearColor];
[selfaddSubview:titleLabel];
floatbutX =kWidthOfButtonMargin;
floatbutY =50;
for(inti =0; i < titleArray.count; i++){
//宽度自适应
NSDictionary*fontDict =@{NSFontAttributeName:[UIFontsystemFontOfSize:kFontSizeOfTabButton]};
CGRectframe_W = [[titleArrayobjectAtIndex:i]boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT)options:NSStringDrawingUsesLineFragmentOriginattributes:fontDictcontext:nil];
if(butX + frame_W.size.width>ViewWidth-kWidthOfButtonMargin*2) {
butX =kWidthOfButtonMargin;
butY +=40;
}
UIButton*but = [[UIButtonalloc]initWithFrame:CGRectMake(butX, butY, frame_W.size.width+10,30)];
[butsetTitle:[titleArrayobjectAtIndex:i]forState:UIControlStateNormal];
[butsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
but.titleLabel.font= [UIFontsystemFontOfSize:kFontSizeOfTabButton];
but.tag= i+1;
[but.layersetMasksToBounds:YES];
[but.layersetCornerRadius:3];
but.layer.borderWidth=0.7;
but.layer.borderColor= [LINECOLORCGColor];
[selfaddSubview:but];
butX =CGRectGetMaxX(but.frame) +kWidthOfButtonMargin;
CGRectrect =self.frame;
rect.size.height= butY +40;
self.frame= rect;
}
_categoryHegith=self.frame.size.height;
UIView*textViewBg = [[UIViewalloc]initWithFrame:CGRectMake(0,40,ViewWidth,self.frame.size.height-40)];
textViewBg.backgroundColor= [UIColorwhiteColor];
[selfaddSubview:textViewBg];
[selfsendSubviewToBack:textViewBg];
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(singleTap:)];
//点击的次数
singleRecognizer.numberOfTapsRequired=1;//单击
//给self.view添加一个手势监测;
[selfaddGestureRecognizer:singleRecognizer];
}
returnself;
}
#pragma mark -关闭键盘
- (void)singleTap:(UITapGestureRecognizer*)recognizer{
//处理单击操作
_endEidtBlock();
}
- (instancetype)initWithFrame:(CGRect)frame{
self= [superinitWithFrame:frame];
if(self) {
}
returnself;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end