UIButton

2018年5月15日

  1. 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)把它分享出去撇簿。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市妒挎,隨后出現(xiàn)的幾起案子厢洞,更是在濱河造成了極大的恐慌仇让,老刑警劉巖典奉,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異丧叽,居然都是意外死亡卫玖,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門踊淳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來假瞬,“玉大人,你說我怎么就攤上這事迂尝⊥衍裕” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵雹舀,是天一觀的道長芦劣。 經(jīng)常有香客問我,道長说榆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任寸认,我火速辦了婚禮签财,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘偏塞。我一直安慰自己唱蒸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布灸叼。 她就那樣靜靜地躺著神汹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪古今。 梳的紋絲不亂的頭發(fā)上屁魏,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天,我揣著相機(jī)與錄音捉腥,去河邊找鬼氓拼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛抵碟,可吹牛的內(nèi)容都是我干的桃漾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拟逮,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼撬统!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起敦迄,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤恋追,失蹤者是張志新(化名)和其女友劉穎凭迹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體几于,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蕊苗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了沿彭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片朽砰。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖喉刘,靈堂內(nèi)的尸體忽然破棺而出瞧柔,到底是詐尸還是另有隱情,我是刑警寧澤睦裳,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布造锅,位于F島的核電站,受9級(jí)特大地震影響廉邑,放射性物質(zhì)發(fā)生泄漏哥蔚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一蛛蒙、第九天 我趴在偏房一處隱蔽的房頂上張望糙箍。 院中可真熱鬧,春花似錦牵祟、人聲如沸深夯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽咕晋。三九已至,卻和暖如春收奔,著一層夾襖步出監(jiān)牢的瞬間掌呜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國打工筹淫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留站辉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓损姜,卻偏偏與公主長得像饰剥,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子摧阅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一汰蓉、簡(jiǎn)介 <<UIButton(按鈕) : 既能顯示文字,又能顯示圖片棒卷,還能隨時(shí)調(diào)整內(nèi)部圖片和文字的位置顾孽,實(shí)現(xiàn)了監(jiān)...
    無邪8閱讀 5,650評(píng)論 0 2
  • 對(duì)象繼承關(guān)系 UIButton 類本身定義繼承 UIControl 祝钢,描述了在 iOS 上所有用戶界面控件的常見基...
    獨(dú)木舟的木閱讀 3,736評(píng)論 0 3
  • 一個(gè)UIButton的實(shí)例變量, 使一個(gè)按鈕(button)在觸摸屏上生效若厚。一個(gè)按鈕監(jiān)聽觸摸事件拦英,當(dāng)被點(diǎn)擊時(shí),給目...
    wushuputi閱讀 1,502評(píng)論 0 1
  • UIButton的官方文檔https://developer.apple.com/reference/uikit/...
    阿斯蘭iOS閱讀 906評(píng)論 0 0
  • 來吧测秸,時(shí)光正在流逝周圍的掌聲已經(jīng)響起疤估, 趁年華未盡,在藍(lán)色大幕下盡情表演霎冯! 讓你我在高山之巔舞蹈铃拇,順便和天風(fēng)嘶吼…...
    a839668842f8閱讀 193評(píng)論 2 7