改变Button内text相对位置的颜色和字体大小
(抱歉没备注,有问题可以私信)
// 创建一个普通Button
CGRect rect = [UIScreen mainScreen].bounds;
UIButton * teamBt = [UIButton buttonWithType:UIButtonTypeSystem];
teamBt.frame = CGRectMake(rect.size.width/2 - rect.size.width/320 *90, 30, rect.size.width/320 *180, 20);
teamBt.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[teamBt addTarget:self action:@selector(thisIsAButton) forControlEvents:UIControlEventTouchUpInside];
//设置按钮居中
[teamBt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
// 只改动后两个字的颜色
NSString *title = [NSString stringWithFormat:@"%@简介",self.teamName];
NSInteger num = title.length -2;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:title];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,num)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:64.0/255 green:154.0/255 blue:156.0/255 alpha:1] range:NSMakeRange(num,2)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:20] range:NSMakeRange(num, 2)];
[teamBt setAttributedTitle:str forState:UIControlStateNormal];
[self.view addSubview:teamBt];