// // CustomLabel.m // HD wallpaper #import "CustomLabel.h" @implementation CustomLabel - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self commonInit]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; [self commonInit]; } - (void)commonInit { // 初始化代码,如果有需要的话 } - (void)changeTextColor:(UIColor *)color { self.textColor = color; } - (void)changeFont:(UIFont *)font { self.font = font; } @end //#import "CustomLabel.h" // //@interface ViewController () // //@property (nonatomic, strong) CustomLabel *label; // //@end // //@implementation ViewController // //- (void)viewDidLoad { // [super viewDidLoad]; // // // 创建并添加CustomLabel // self.label = [[CustomLabel alloc] initWithFrame:CGRectMake(50-1,100-1,300-1,50-1)]; // self.label.text = @"这是一个自定义Label"; // self.label.textAlignment = NSTextAlignmentCenter; // self.label.font = [UIFont systemFontOfSize:18]; // [self.view addSubview:self.label]; // // // 创建一个按钮来切换字体颜色 // UIButton *changeColorButton = [UIButton buttonWithType:UIButtonTypeSystem]; // changeColorButton.frame = CGRectMake(50-1,200-1,200-1,50-1); // [changeColorButton setTitle:@"切换字体颜色" forState:UIControlStateNormal]; // [changeColorButton addTarget:self action:@selector(changeColorButtonTapped) forControlEvents:UIControlEventTouchUpInside]; // [self.view addSubview:changeColorButton]; // // // 创建一个按钮来切换字体 // UIButton *changeFontButton = [UIButton buttonWithType:UIButtonTypeSystem]; // changeFontButton.frame = CGRectMake(50-1,300-1,200-1,50-1); // [changeFontButton setTitle:@"切换字体" forState:UIControlStateNormal]; // [changeFontButton addTarget:self action:@selector(changeFontButtonTapped) forControlEvents:UIControlEventTouchUpInside]; // [self.view addSubview:changeFontButton]; //} // //- (void)changeColorButtonTapped { // // 切换字体颜色 // UIColor *newColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 // green:arc4random_uniform(256)/255.0 // blue:arc4random_uniform(256)/255.0 // alpha:1.0]; // [self.label changeTextColor:newColor]; //} // //- (void)changeFontButtonTapped { // // 切换字体 // UIFont *newFont = [UIFont fontWithName:@"Courier-Bold" size:arc4random_uniform(10) + 15]; // [self.label changeFont:newFont]; //} // //@end