UIButton

2018年5月15日

  1. btn.titleLabel.font = [UIFont systemFontOfSize:22];

2017年9月27日
1.xib按鈕 圖片背景顏色顯示錯誤修改


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,用下面的這種設置
        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. 根據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日
一.按鈕設置富文本顯示
效果:

image.png

2.實現

//實現字體不同大小效果
            NSMutableAttributedString *attributedStr = [self getOneMoreBtnChangeText:num];
            [_oneMoreB setAttributedTitle:attributedStr forState:UIControlStateNormal];
- (NSMutableAttributedString *)getOneMoreBtnChangeText:(NSInteger)num
{
    NSString *part1 = _oneMoreB.titleLabel.text;
    NSString *part2 = [NSString stringWithFormat:@"(%ld次重考機會)",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;
}

二.不可點擊按鈕變灰色

image.png

實現

if (num > 0) {
        _oneMoreB.enabled = YES;
        _oneMoreB.alpha = 1.0;
    }else{
        _oneMoreB.enabled = NO;
        _oneMoreB.alpha = 0.4;
    }

2017年4月18日
一.創建圓形按鈕(答題卡)實現
1.效果


Paste_Image.png

2.主要實現代碼
2.1按鈕控件

//題卡按鈕類型
typedef NS_ENUM(NSInteger, HuExerciseCardBtnType){
    HuExerciseCardBtnTypeNoDone,//默認題卡樣式沒答題(答題)
    HuExerciseCardBtnTypeDone,//答過題(答題)
    HuExerciseCardBtnTypeWrong,//答錯題
    HuExerciseCardBtnTypeRight,//答對題
};

+ (HuButton *)exerciseCardBtn:(HuExerciseCardBtnType)btnType
{
    HuButton *btn = [[HuButton alloc] init];
    //默認是錯誤的樣式
    [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創建各個樣式答題卡

- (void)initContentView
{
    _scrollView = [[UIScrollView alloc]init];
    _scrollView.backgroundColor = fontcolor_C1;
    [self.view addSubview:_scrollView];

    //添加序號按鈕
    UIButton *lastBtn = nil;
    NSInteger row = 0;
    CGRect frame;


    for(int i = 0; i < [_allExerciseShowIdArr count]; i++)
    {
        //設置答過題目按鈕的樣式
        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];

        //設置當前位置的小紅點
        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;
    }

    //設置_scrollView滾動相關屬性
    if (row > 0) {
        CGFloat contentHeight = lastBtn.bottom + eCard_btn_vFlap;
        CGFloat viewHeight = eCard_contentView_height;
        //需要滾動
        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日
一.整行按鈕 如何實現等間距顯示
1.效果:


Paste_Image.png

2.實現:

    NSArray *title=[[NSArray alloc]initWithObjects:@"電子會刊",@"介紹",@"嘉賓",@"獲獎名單",@"會務咨詢" ,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日
一.按鈕點擊區域太小解決,添加一個透明按鈕
1.效果:

Paste_Image.png

2.實現(添加一個透明按鈕)

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

    //添加一個透明按鈕擴大點擊區域
    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.延遲點擊

UIButton * button = [_tabBarBgView viewWithTag:1];
[self performSelector:@selector(btnClick:) withObject:button afterDelay:5];

如果您發現本文對你有所幫助,如果您認為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、簡介 <<UIButton(按鈕) : 既能顯示文字,又能顯示圖片,還能隨時調整內部圖片和文字的位置,實現了監...
    無邪8閱讀 5,694評論 0 2
  • 對象繼承關系 UIButton 類本身定義繼承 UIControl ,描述了在 iOS 上所有用戶界面控件的常見基...
    獨木舟的木閱讀 3,772評論 0 3
  • 一個UIButton的實例變量, 使一個按鈕(button)在觸摸屏上生效。一個按鈕監聽觸摸事件,當被點擊時,給目...
    wushuputi閱讀 1,545評論 0 1
  • UIButton的官方文檔https://developer.apple.com/reference/uikit/...
    阿斯蘭iOS閱讀 948評論 0 0
  • 來吧,時光正在流逝周圍的掌聲已經響起, 趁年華未盡,在藍色大幕下盡情表演! 讓你我在高山之巔舞蹈,順便和天風嘶吼…...
    a839668842f8閱讀 212評論 2 7