Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:03:52+00:00 2026-05-16T07:03:52+00:00

Multiple colours in an NSString or NSMutableStrings are not possible. So I’ve heard a

  • 0

Multiple colours in an NSString or NSMutableStrings are not possible. So I’ve heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta.

I would like to have a string that has three colours.

The reason I don’t use 3 separate NSStrings, is because the length of each of the three NSAttributedString substrings changes often and so I would prefer, not to use any calculations to re-position 3 separate NSString objects.

If it’s possible using NSAttributedString how do I make the following – (if not possible with NSAttributed string how would you do it):

alt text

Edit:
Remember, @"first", @"second" and @"third" will be replaced by other strings at any time. So using hardcoded NSRange values won’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:03:53+00:00Added an answer on May 16, 2026 at 7:03 am

    When building attributed strings, I prefer to use the mutable subclass, just to keep things cleaner.

    That being said, here’s how you create a tri-color attributed string:

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
    

    typed in a browser. caveat implementor

    Obviously you’re not going to hard-code in the ranges like this. Perhaps instead you could do something like:

    NSDictionary *wordToColorMapping = ....;  //an NSDictionary of NSString => UIColor pairs
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@""];
    for (NSString *word in wordToColorMapping) {
      UIColor *color = [wordToColorMapping objectForKey:word];
      NSDictionary *attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
      NSAttributedString *subString = [[NSAttributedString alloc] initWithString:word attributes:attributes];
      [string appendAttributedString:subString];
      [subString release];
    }
    
    //display string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table which stores products. Each product can have multiple colours.
I am developing an application which displays multiple views as tables (for example customers,
Is it possible to have multiple font colors in matplotlib figure titles? Something like
Is it possible to have multiple drop down lists in asp.net mvc? What I'm
Is it possible to have multiple formatting of a text string within a single
I would like to display multiple colors (and potentially shapes and sizes) of data
I'm looking for a way to display multiple colors in a single C#/.NET label.
My project has multiple themes with different colors. I need to skin certain textboxes
Icon files (*.ico) may contain multiple images at different sizes and of different colour
Multiple part question on running gvim on windows xp. I have a _vimrc file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.