首页 > 代码库 > 选中按钮改变文字大小和颜色

选中按钮改变文字大小和颜色

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC" } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; min-height: 25.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #cf8724 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #e82300 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #36568a } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #36568a; min-height: 16.0px } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #777897 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #587ea8 } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #cf8724 } p.p12 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c35900 } p.p13 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #36568a } p.p14 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000 } span.s1 { } span.s2 { color: #777897 } span.s3 { color: #36568a } span.s4 { color: #c35900 } span.s5 { color: #e82300 } span.s6 { color: #000000 } span.s7 { font: 14.0px Menlo } span.s8 { color: #587ea8 } span.s9 { font: 14.0px Menlo; color: #000000 } span.s10 { font: 14.0px "PingFang SC"; color: #e82300 }</style>

这是一个比较实用的小功能,可以根据项目的需求而改变按钮的属性,这里我只是针对我的项目写出来的一个简单demo,需要想给在外部控制button的颜色和文字大小,可以给他封装一个方法或者添加两个属性即可。代码奉上:

 

这个功能的效果如下:

 

 技术分享

 

 

//

//  YMChangeButton.h

//

 

#import <UIKit/UIKit.h>

 

@interface YMChangeButton : UIButton

 

@end

 

//

//  YMChangeButton.m

//

 

#import "YMChangeButton.h"

 

#define LabelFontRegular(t) [UIFont fontWithName:@"PingFangHK-Regular" size:(t)]

 

@implementation YMChangeButton

 

//xib

- (void)awakeFromNib

{

    [super awakeFromNib];

}

 

//纯代码

- (instancetype)initWithFrame:(CGRect)frame

{

    if ([super initWithFrame:frame]) {

        

    }

    return self;

}

 

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    NSRange range = [self.titleLabel.text rangeOfString:self.titleLabel.text];

    

    //设置普通状态下的文字属性

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];

    [attributedStr addAttribute:NSFontAttributeName value:LabelFontRegular(15) range:range];

    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:range];

    [self setAttributedTitle:attributedStr forState:UIControlStateNormal];

    

    //设置选中状态下的文字属性

    NSMutableAttributedString *attributedStrS = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];

    [attributedStrS addAttribute:NSFontAttributeName value:LabelFontRegular(20) range:range];

    [attributedStrS addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];

    [self setAttributedTitle:attributedStrS forState:UIControlStateSelected];

    

}

 

@end

 

 

这个类的使用方法如下:

//

//  ViewController.m

//

 

#import "ViewController.h"

#import "UIView+MHExtension.h"

#import "YMChangeButton.h"

 

@interface ViewController ()

 

/** 当前选中的标题按钮 */

@property (nonatomic, weak) YMChangeButton *selectedTitleButton;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    CGFloat buttonW = 200;

    CGFloat buttonH = 60;

    for (int i = 0; i < 5; i++) {

        YMChangeButton *button = [[YMChangeButton alloc] init];

        button.mh_x = 100;

        button.mh_y = i * (buttonH + 50) + 100;

        button.mh_width = buttonW;

        button.mh_height = buttonH;

        

        button.backgroundColor = [UIColor greenColor];

        [button setTitle:@"测试" forState:UIControlStateNormal];

        

        button.tag = i;

        

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

        

        [self.view addSubview:button];

    }

    

    NSLog(@"%@",self.view.subviews);

    YMChangeButton *firstTileButton = self.view.subviews.lastObject;

    firstTileButton.selected = YES;

    self.selectedTitleButton = firstTileButton;

}

 

- (void)buttonClick:(YMChangeButton *)sender

{

    

    self.selectedTitleButton.selected = NO;

    sender.selected = YES;

    self.selectedTitleButton = sender;

    

    

    NSLog(@"111");

}

 

虽然这个功能写的不怎么样,但是对于新手来说也是不错的demo,欢迎各位大神指教。

选中按钮改变文字大小和颜色