2018年5月15日
- btn.titleLabel.font = [UIFont systemFontOfSize:22];
2017年9月27日
1.xib按鈕 圖片背景顏色顯示錯(cuò)誤修改
image.png
2017年9月19日
1.圖片和文字布局修改
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space {
// 1. 得到imageView和titleLabel的寬、高
// CGFloat imageWith = self.imageView.frame.size.width;
// CGFloat imageHeight = self.imageView.frame.size.height;
CGFloat imageWith = self.currentImage.size.width;
CGFloat imageHeight = self.currentImage.size.height;
CGFloat labelWidth = 0.0;
CGFloat labelHeight = 0.0;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// 由于iOS8中titleLabel的size為0,用下面的這種設(shè)置
labelWidth = self.titleLabel.intrinsicContentSize.width;
labelHeight = self.titleLabel.intrinsicContentSize.height;
} else {
labelWidth = self.titleLabel.frame.size.width;
labelHeight = self.titleLabel.frame.size.height;
}
// 2. 聲明全局的imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
// 3. 根據(jù)style和space得到imageEdgeInsets和labelEdgeInsets的值
switch (style) {
case MKButtonEdgeInsetsStyleTop: {
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space, 0);
}
break;
//圖片在左
case MKButtonEdgeInsetsStyleLeft: {
imageEdgeInsets = UIEdgeInsetsMake(0, -space, 0, space);
labelEdgeInsets = UIEdgeInsetsMake(0, space, 0, -space);
}
break;
case MKButtonEdgeInsetsStyleBottom: {
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space, -imageWith, 0, 0);
}
break;
case MKButtonEdgeInsetsStyleRight: {
imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space, 0, -labelWidth-space);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space, 0, imageWith+space);
}
break;
default:
break;
}
// 4. 賦值
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}
2017年7月22日
1.獲取按鈕title
NSString *title = self.resendButton.currentTitle;
2017年5月27日
一.按鈕設(shè)置富文本顯示
效果:
image.png
2.實(shí)現(xiàn)
//實(shí)現(xiàn)字體不同大小效果
NSMutableAttributedString *attributedStr = [self getOneMoreBtnChangeText:num];
[_oneMoreB setAttributedTitle:attributedStr forState:UIControlStateNormal];
- (NSMutableAttributedString *)getOneMoreBtnChangeText:(NSInteger)num
{
NSString *part1 = _oneMoreB.titleLabel.text;
NSString *part2 = [NSString stringWithFormat:@"(%ld次重考機(jī)會(huì))",num];
NSInteger len1 = part1.length;
NSInteger len2 = part2.length;
NSString *str = [NSString stringWithFormat:@"%@%@",part1,part2];
NSMutableAttributedString *attributedStr = nil;
NSInteger length = len1;
NSInteger index = 0;
attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
[attributedStr addAttribute:NSFontAttributeName
value:fontsize_T1
range:NSMakeRange(index, length)];
index = index + length;
length = len2;
[attributedStr addAttribute:NSFontAttributeName
value:fontsize_T5
range:NSMakeRange(index, length)];
return attributedStr;
}
二.不可點(diǎn)擊按鈕變灰色
image.png
實(shí)現(xiàn)
if (num > 0) {
_oneMoreB.enabled = YES;
_oneMoreB.alpha = 1.0;
}else{
_oneMoreB.enabled = NO;
_oneMoreB.alpha = 0.4;
}
2017年4月18日
一.創(chuàng)建圓形按鈕(答題卡)實(shí)現(xiàn)
1.效果
Paste_Image.png
2.主要實(shí)現(xiàn)代碼
2.1按鈕控件
//題卡按鈕類型
typedef NS_ENUM(NSInteger, HuExerciseCardBtnType){
HuExerciseCardBtnTypeNoDone,//默認(rèn)題卡樣式?jīng)]答題(答題)
HuExerciseCardBtnTypeDone,//答過題(答題)
HuExerciseCardBtnTypeWrong,//答錯(cuò)題
HuExerciseCardBtnTypeRight,//答對(duì)題
};
+ (HuButton *)exerciseCardBtn:(HuExerciseCardBtnType)btnType
{
HuButton *btn = [[HuButton alloc] init];
//默認(rèn)是錯(cuò)誤的樣式
[btn setBackgroundColor:[UIColor clearColor]];
[btn.layer setBorderWidth:list_borderLine_height];
UIColor *color;
if(btnType == HuExerciseCardBtnTypeRight)
{
color = [HuConfigration uiColorFromString:@"#0acd9f"];
}
else if(btnType == HuExerciseCardBtnTypeWrong)
{
color = [HuConfigration uiColorFromString:@"#ff6969"];
}
else if(btnType == HuExerciseCardBtnTypeNoDone)
{
//沒有答過的樣式
[btn setBackgroundColor:eCard_undo_color];
color = [HuConfigration uiColorFromString:@"#484848"];
[btn.layer setBorderWidth:0];
}
else if (btnType == HuExerciseCardBtnTypeDone)
{
//答過題目樣式
[btn setBackgroundColor:eCard_do_color];
color = fontcolor_C1;
[btn.layer setBorderWidth:0];
}
btn.layer.borderColor = color.CGColor;
[btn setTitleColor:color forState:UIControlStateNormal];
return btn;
}
2.2創(chuàng)建各個(gè)樣式答題卡
- (void)initContentView
{
_scrollView = [[UIScrollView alloc]init];
_scrollView.backgroundColor = fontcolor_C1;
[self.view addSubview:_scrollView];
//添加序號(hào)按鈕
UIButton *lastBtn = nil;
NSInteger row = 0;
CGRect frame;
for(int i = 0; i < [_allExerciseShowIdArr count]; i++)
{
//設(shè)置答過題目按鈕的樣式
NSString *curID = _allExerciseShowIdArr[i];
HuExerciseCardBtnType btnType = HuExerciseCardBtnTypeNoDone;
if ([_allUserAnswerExerciseShowIdArr containsObject:curID]) {
btnType = HuExerciseCardBtnTypeDone;
}
if(_pageType == HuTestPracticePageTypeAnalyse)
{
if (i < _resExercises.count) {
btnType = _resExercises[i].result ? HuExerciseCardBtnTypeRight : HuExerciseCardBtnTypeWrong;
}
}
HuButton *btn = [HuButton exerciseCardBtn:btnType];
[_scrollView addSubview:btn];
CGFloat yPos;
CGFloat xPos;
if(lastBtn == nil){
yPos = eCard_btn_vFlap;
xPos = common_margin;
if(row==0){row = 1;}
}else if(lastBtn.right + eCard_btn_hFlap + eCard_btn_width > HHBWIDTH){
yPos = lastBtn.bottom + eCard_btn_vFlap;
xPos = common_margin;
row++;
}else{
yPos = lastBtn.top;
xPos = lastBtn.right + eCard_btn_hFlap;
}
frame = CGRectMake(xPos, yPos, eCard_btn_width, eCard_btn_height);
btn.frame = frame;
btn.layer.cornerRadius = eCard_btn_width/2.0;
[btn setTitle:curID forState:UIControlStateNormal];
btn.tag = tag_begin_index + i;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//設(shè)置當(dāng)前位置的小紅點(diǎn)
if(i == _eId)
{
UILabel * redLabel = [[UILabel alloc] init];
redLabel.backgroundColor = [UIColor redColor];
CGFloat width = 7;
redLabel.layer.cornerRadius = width/2;
redLabel.layer.masksToBounds = YES;
CGRect frame = CGRectMake(btn.centerX - width/2.0, btn.bottom, width, width);
redLabel.frame = frame;
[_scrollView addSubview:redLabel];
}
lastBtn = btn;
}
//設(shè)置_scrollView滾動(dòng)相關(guān)屬性
if (row > 0) {
CGFloat contentHeight = lastBtn.bottom + eCard_btn_vFlap;
CGFloat viewHeight = eCard_contentView_height;
//需要滾動(dòng)
if (contentHeight > viewHeight) {
_scrollView.contentSize = CGSizeMake(HHBWIDTH, contentHeight);
_scrollView.scrollEnabled = YES;
}else{
_scrollView.scrollEnabled = NO;
}
_scrollView.frame = CGRectMake(0, 0, HHBWIDTH, MIN(contentHeight,viewHeight));
}
}
2017年4月17日
一.整行按鈕 如何實(shí)現(xiàn)等間距顯示
1.效果:
Paste_Image.png
2.實(shí)現(xiàn):
NSArray *title=[[NSArray alloc]initWithObjects:@"電子會(huì)刊",@"介紹",@"嘉賓",@"獲獎(jiǎng)名單",@"會(huì)務(wù)咨詢" ,nil];
CGFloat viewWidth = HHBWIDTH - 2*common_margin;
CGFloat allBtnViewWidth = 0;
UIFont *fontSize = fontsize_T2;
NSMutableArray *btnViewWidthArr = @[].mutableCopy;
for (NSInteger i = 0; i < title.count; i++) {
CGSize size = [title[i] sizeWithAttributes: @{NSFontAttributeName:fontSize}];
allBtnViewWidth += size.width;
[btnViewWidthArr addObject:@(size.width)];
}
CGFloat flap = (viewWidth - allBtnViewWidth)/(title.count - 1);
if (flap < 0) {
flap = 0;
}
CGFloat xPos = common_margin;
CGFloat yPos = 25;
CGFloat width = 0;
CGFloat height = 30;
for (int i = 0; i < title.count; i ++) {
//////////
width = [btnViewWidthArr[i] floatValue];
navigationBtn.frame = CGRectMake(xPos, yPos, width, height);
xPos += width + flap;
[_navigationView addSubview:navigationBtn];
}
2017年3月14日
一.按鈕點(diǎn)擊區(qū)域太小解決擂煞,添加一個(gè)透明按鈕
1.效果:
Paste_Image.png
2.實(shí)現(xiàn)(添加一個(gè)透明按鈕)
xPos = HHBWIDTH - common_margin - filterBtn_width;
yPos = (practice_filterView_height - filterBtn_height)/2.0;
frame = CGRectMake(xPos, yPos, filterBtn_width, filterBtn_height);
_filterBtn = [[UIButton alloc] initWithFrame:frame];
[_filterBtn setImage:IMG(@"filterBtn") forState:UIControlStateNormal];
[_filterBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:_filterBtn];
//添加一個(gè)透明按鈕擴(kuò)大點(diǎn)擊區(qū)域
width = 50;
height = practice_filterView_height;
xPos = HHBWIDTH - width;
yPos = 0;
frame = CGRectMake(xPos, yPos, width, height);
UIButton *enlargeBtn = [[UIButton alloc] initWithFrame:frame];
[enlargeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:enlargeBtn];
[self.view addSubview:bgView];
2017年1月11日
1.延遲點(diǎn)擊
UIButton * button = [_tabBarBgView viewWithTag:1];
[self performSelector:@selector(btnClick:) withObject:button afterDelay:5];
如果您發(fā)現(xiàn)本文對(duì)你有所幫助屋厘,如果您認(rèn)為其他人也可能受益鳖宾,請(qǐng)把它分享出去撇簿。