johnbakers
johnbakers

Reputation: 24771

Formatting text within UILabel differently

I'd like different words in a UILabel to be different colors. Does this mean each word will need to be a different UILabel? I'm guessing yes, though sure would be nice to just put color codes in the label somehow, you know? I guess I'm a bit spoiled by text markup in HTML.

Upvotes: 4

Views: 2978

Answers (3)

Rob Napier
Rob Napier

Reputation: 299623

There is no proper UIRichTextView in iOS. It's high on my wish-list for iOS 6 (and there's some reason to believe we may get it then due to the release of Pages).

Your options are to use multiple UILabel views, NSString UIKit Additions, Core Text, UIWebView, or one of a few third-party frameworks such as:

  • NSAttributedString-Additions-for-HTML
  • CoreTextWrapper
  • OHAttributedLabel
  • OmniUI

All of the current solutions have different problems. The most common problem is that it's hard to get select and copy functionality to work with rich text unless you use a web view. Web views are incredibly annoying because they're asynchronous and you have to do a lot of your interactions in JavaScript.

I wish there were a better answer.

(Obligatory shilling: This topic is covered in depth in Chapter 18 of iOS 5 Programming Pushing the Limits.)

Upvotes: 5

user843337
user843337

Reputation:

As far as I'm aware you'd need to have separate labels for each different coloured word. Depending what you're trying to do you may be able to make use of myLabel.textColor to change the colour of the periodically or on events etc.

Upvotes: 0

Alastair Stuart
Alastair Stuart

Reputation: 4185

UILabel doesn't support segmented formatting (the entire thing can only have one format).

Have a look at OHAttributedLabel, which does what you want.

Upvotes: 2

Related Questions