Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Button title in two different colors in iOS

I want to set title on Button in two colors.

Is it possible same as given image below ?

Button in 2 colors

Any help will be appreciated.

Thanks in advance.

like image 545
Manann Sseth Avatar asked Jun 27 '13 10:06

Manann Sseth


2 Answers

This is what you need

  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];
like image 115
Vivek Sehrawat Avatar answered Sep 28 '22 07:09

Vivek Sehrawat


In storyBoard : Follow the ScreenShot.

enter image description here

enter image description here

enter image description here

For Programmatically : There are already many answers posts over here for attributed text and If you want to put BackgroundImage for UIButton Set a button background image iPhone programmatically

like image 23
Kumar KL Avatar answered Sep 28 '22 08:09

Kumar KL