Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Multi textcolor in label

Hello!
I want to make a multi-colored label with the text so that each line had a different color
The label should be this one, because it lies on a scroll
Help me please!
Tagged:

Replies

  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    Apple has been very slowly adding support for styled text to iOS.

    Starting in iOS 6, the OS supports the NSAttributedString class, and UILabel now has an NSAttributedString property called attributedText that can be used to install styled text into a label.

    If you want your program to work in iOS 5, you can't use NSAttributedString. Instead you have to use the lower-level Core Text framework. That's fairly tricky however.

    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • baja_yubaja_yu San FranciscoPosts: 6,215Moderators, Registered Users Admin
    You can use the attributedText property, but it's only available since iOS 6. Otherwise you'll have to use something else, either find a third party component that can do this, draw the text yourself or use CoreText.
  • paparotnikpaparotnik Posts: 2New Users
    Thank you, I have learned a list of methods in this class, and found nothing related to the color of the text, the only thing that seems to be what it setAttributes, but I do not know how to properly use
  • baja_yubaja_yu San FranciscoPosts: 6,215Moderators, Registered Users Admin
    If you look at the documentation for attributedText in UILabel: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html you will see that it takes an NSAttributedString object. So check out the docs for that to see how to create it.
  • givensurgivensur On the Information SuperhighwayPosts: 541Registered Users @ @ @
    attributes is an NSDictionary, that takes keys described in the Constants section of the NSAttributedString UIKit Additions Reference.

    Example:
    
    NSMutableAttributedString *colorString = [[NSMutableAttributedString alloc] initWithString:@"foobar"];
    
    NSDictionary *fooAttributes = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    NSDictionary *barAttributes = @{ NSForegroundColorAttributeName: [UIColor greenColor] };
    
    NSRange fooRange = [colorString.string rangeOfString:@"foo"];
    NSRange barRange = [colorString.string rangeOfString:@"bar"];
    
    [colorString setAttributes:fooAttributes range:fooRange];
    [colorString setAttributes:barAttributes range:barRange];
    
    I'm not actually sitting at a Mac right now though, so the above code may have mistakes that I didn't catch.
    I make cake apps.
Sign In or Register to comment.