不規(guī)則按鈕排列

下邊是.h和.m文件,需要源碼聯(lián)系本人加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++){

//寬度自適應(yīng)

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:)];

//點擊的次數(shù)

singleRecognizer.numberOfTapsRequired=1;//單擊

//給self.view添加一個手勢監(jiān)測;

[selfaddGestureRecognizer:singleRecognizer];

}

returnself;

}

#pragma mark -關(guān)閉鍵盤

- (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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容