1

I want to make parts of the text of a UILabel appears with different background colour. I have seen some examples changing the Font and text colour but I could not find any thing about changing background colour. Is there any possible way to do that?

Thanks.

If it possible to provide a code sample it will be great.

Ismail
  • 2,778
  • 2
  • 24
  • 39
  • 3
    Using `NSAttributedString`? – trojanfoe Apr 16 '15 at 15:04
  • `NSAttributedString` should work for you. But if the change in background color isn't meant to occur directly between two characters then use another view behind the label and set that view's background color. – rmaddy Apr 16 '15 at 15:07
  • @trojanfoe Yes the examples I have seen were using `NSAttributedString`. What I want to do Is to highlight some text inside the `UILabel`. – Ismail Apr 16 '15 at 15:08

1 Answers1

2

Yes, use attributed strings.

  let textColor = UIColor(red: 0.175, green: 0.458, blue: 0.831, alpha: 1)
  let attributes = [
    NSForegroundColorAttributeName : textColor,
    NSFontAttributeName : font,
    NSTextEffectAttributeName : NSTextEffectLetterpressStyle
  ]
  let attributedString = NSAttributedString(string: note.title, attributes: attributes)

  labelText.attributedText = attributedString

// Edit for ObjC Answer

NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:@"you string"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];//TextColor
[string addAttribute:NSUnderlineStyleAttributeName value:underlineNumber range:NSMakeRange(0, string.length)];//Underline color
[string addAttribute:NSUnderlineColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, string.length)];//TextColor
labelText.attributedText = string;
Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Is there an `Objective-c` version of this code? also it seems to me it will change the foreground colour `NSForegroundColorAttributeName`. – Ismail Apr 16 '15 at 15:15
  • 1
    I didn't provide code to do exactly what you want. That's on you. I provided an example of how attributed strings work in Swift and ObjC. Dig into the docs for all possibilities to further understand the POWER of attributed strings. ;-) https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/ – Mark McCorkle Apr 16 '15 at 15:17
  • Thank you for the provided example and the documentation. – Ismail Apr 16 '15 at 15:23
  • 1
    @DCGoD, don't you mean `NSBackgroundColorAttributeName `? – n00bProgrammer Apr 16 '15 at 15:27
  • 1
    @Ismail, check out the [attributes](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/doc/constant_group/Character_Attributes) for all the attributes you can modify for an attributed string. – n00bProgrammer Apr 16 '15 at 15:29
  • I've got it. it's following @DCGoD Example with attribute like @n00bProgrammer mentioned. `[string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, string.length)];` – Ismail Apr 16 '15 at 15:37