在iOS中以两种不同的颜色设置Button标题

MS.*_*MS. 5 iphone uibutton uicolor ios

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

它可能与下面给出的图像相同吗?

按钮有2种颜色

任何帮助将不胜感激.

提前致谢.

Viv*_*wat 15

这就是你需要的

  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];
Run Code Online (Sandbox Code Playgroud)


Kum*_* KL 12

在storyBoard中:关注ScreenShot.

在此输入图像描述

在此输入图像描述

在此输入图像描述

对于编程:这里已经有很多答案帖子在这里为属性文本和如果你想为UIButton 设置BackgroundImage 设置一个按钮背景图像iPhone以编程方式