UIButton按鈕的背景圖和字體顏色

 平時使用UIButton不太注意,現在發現很好使用,分享給大家:

        //背景圖和文字顏色

        [button setBackgroundImage:[UIImage imageNamed:@"btnbt.png"] forState:

UIControlStateNormal

];

        [button setBackgroundImage:[UIImage imageNamed:@"btnb-on.png"] forState:UIControlStateSelected];

        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];

        [button addTarget:self action:@selector(chooseParticipants:) forControlEvents:UIControlEventTouchUpInside];

        //文字靠左對齊

        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

        //文字距離左側的像素          {top, left, bottom, right};

        button.contentEdgeInsets = UIEdgeInsetsMake(0,2, 0, 0);

        //文字字體大小

        button.titleLabel.font = [UIFont systemFontOfSize:13.0f];



                

#pragma mark ~~~~~~ 選中參與人員後的方法 ~~~~~~~

-(void)chooseParticipants:(UIButton *)sender{

 

    if (sender.selected == YES) {

        sender.selected = NO;

    }else{

        sender.selected = YES;

    }

  

}

主要代碼已給出,希望對平時不太注意的朋友有所幫助。
發佈了12 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章