在iOS中将按钮标题设置为两种不同的颜色 [英] Set Button title in two different colors in iOS

查看:53
本文介绍了在iOS中将按钮标题设置为两种不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两种颜色在Button上设置标题.

I want to set title on Button in two colors.

是否可能与下面的给定图像相同?

Is it possible same as given image below ?

任何帮助将不胜感激.

谢谢.

推荐答案

这就是您需要的

  UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  btn.frame = CGRectMake(5,10,300,20);
  [self.view addSubview:btn];

  NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"100 Photos"]];
  [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
  [btn setAttributedTitle:attrStr forState:UIControlStateNormal];

这篇关于在iOS中将按钮标题设置为两种不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆