How to change color of some word in a text?

Discussion in 'iOS Programming' started by Kaisser, May 15, 2012.

  1. Kaisser macrumors newbie

    Joined:
    May 15, 2012
    #1
    Hello all.

    I have a problem.
    I need to change the color of a word in a text. One word of a text in a color and the whole text, excep the word, in another color.

    is it possible?

    Thanks!
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Not with any of the standard text UI elements on the iPhone.

    Personally, I use UIWebViews to get rich formatted text in iOS apps. Just have them load a local html file.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Perhaps. But there is the NSAttributedString class that can be leveraged. Using it is rather tricky (just read through the Class Reference), so I suggest using a third-party library class to make things easier. I've used TTTAttributedLabel by Matt Thompson.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Ah. I've always been reluctant to use third party libraries. I feel like it's cheating somehow to use those even though I'd use the same library if it came from Apple.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I don't feel it's cheating. I feel it's not "reinventing the wheel". I do, however, make sure to look through the source extensively to get a very good grasp of how it works. One should rarely incorporate third-party code without a good understanding of what it does.
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    I also do this by using some HTML inside a small textfield-sized UIWebView. Only a few extra lines of code required, and one can write a small custom class to do even that little bit.
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008

Share This Page