iOS Quick Tip: Create a UILabel Where Each Letter in the String is a Different Color

Stephen Zaharuk / Tuesday, June 17, 2014

In iOS 6, Apple introduced the NSMutableAttributedString class. Along with it, all of their UIViews which previously exposed a text property, now expose an "attributedText" property. 

Recently I was working on an App, where the design required each character in a string to be a different color. Using attributed strings it turned out to be really simple, so i thought i'd share my solution with you today. 

You can simply copy this method into your project and use it as necessary, it just requires the string that you want in technicolor and the array of colors it should use. Note, if you have more characters in your string than you do colors in your array, it will simply wrap the colors around. 

+(NSMutableAttributedString*)buildString:(NSString*)txt withColors:(NSArray*)colors
   NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc]initWithString:txt];

   NSInteger count = attrString.length;

   for(int i = 0;i < count; i++)
      NSRange range;
      range.location = i;
      range.length = 1;

      int colorIndex = i;
      while(colors.count <= colorindex="" br="">      {
         colorIndex -= colors.count;

      UIColor* color = colors[colorIndex];
      [attrString addAttribute:NSForegroundColorAttributeName value:color range:range];

   return attrString;


By Stephen Zaharuk (SteveZ)