美文网首页
改变Button内text相对位置的颜色和字体大小

改变Button内text相对位置的颜色和字体大小

作者: 小专注 | 来源:发表于2016-05-03 10:19 被阅读503次

    (抱歉没备注,有问题可以私信)

    // 创建一个普通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];

    相关文章

      网友评论

          本文标题:改变Button内text相对位置的颜色和字体大小

          本文链接:https://www.haomeiwen.com/subject/ayglrttx.html