Uilabel with Two Different Color Text

Use multiple font colors in a single label

Reference from here.

First of all initialize of you NSString and NSMutableAttributedString as below.

var myString:NSString = "I AM KIRIT MODI"
var myMutableString = NSMutableAttributedString()

In ViewDidLoad

override func viewDidLoad() {

myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
// set label Attribute
labName.attributedText = myMutableString
super.viewDidLoad()
}

OUTPUT

enter image description here

MULTIPLE COLOR

Add the line code below in your ViewDidLoad to get multiple colors in a string.

 myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:10,length:5))

Multiple color OUTPUT

enter image description here

Swift 4

var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedStringKey.font :UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4))

Swift 5.0

 var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedString.Key.font :UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4))

UILabel Text with Multiple Font Colors

UILabel doesnot supprt this property...

Use should use NSAttributedString... and use controllers for drawing NSAttributesString...

Controller for NSAttributedString

UPDATE:

From iOS 6 you can do the following :

label.attributedText = attributedString;

UILabel with two different color text

You can't do this within a UILabels. But my suggestion is that instead of using multiple UILabel just concentrate on NSAttributedString. Find UIControllers that draw NSAttributedString because UILabel, UITextView do not support NSAttributedString.

PS: if you plan to distribute an iOS6 or later application, as UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.

How to make a single UILabel with multiple colors

U should use attributes... like this

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:yourString];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, TXTTOBEBLACKLENGTH)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(TXTTOBEBLACKLENGTH, TXTTOBEBLBLUELENGTH)];
[lblPostContent setAttributedText: text];

UILabel with text of different color in Storyboard

To achieve that in storyboard, make the label's text attributed, then select the range and change colors accordingly.

See the image below:

enter image description here

Hope this helps.

Two colors for UILabel TEXT

you can set text color with pattern image like bellow..

        [yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]];

and also set different color with this bellow code.. please check tutorial with detail mate..

NSString *test = @"Hello. That is a test attributed string.";

CFStringRef string = (CFStringRef) test;
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string);

/*
Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its
attributes right when we create it. We can change them if we use mutable form of CFAttributeString.
*/

//Lets choose the colors we want to have in our string
CGColorRef _orange=[UIColor orangeColor].CGColor;
CGColorRef _green=[UIColor greenColor].CGColor;
CGColorRef _red=[UIColor redColor].CGColor;
CGColorRef _blue=[UIColor blueColor].CGColor;

//Lets have our string with first 20 letters as orange
//next 20 letters as green
//next 20 as red
//last remaining as blue
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange);
CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green);
CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);
CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue);

for more information see this tutorial....

coretext-tutorial-for-ios-part

i hope this help you...

In UILabel, multiple strings with multiple colors for each string?

this, but this is an NSMutableAttributedString, you'll have to figure out how to work with these, however, whatever you are using this string for, you will most like have something exposed from the super class header that exposes an AttributedText property where you would assign this property the value of "myString1" and then you WOULD NOT supply a value for the "text" property.

var myString1 = NSMutableAttributedString(string:"hello how are you buddy")

let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)

let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let myString1Color2 = UIColor(red: 0.000000, green: 0.176471, blue: 0.600000, alpha: 1.000000)
let myString1Color3 = UIColor(red: 0.333333, green: 0.556863, blue: 0.156863, alpha: 1.000000)
let myString1Color4 = UIColor(red: 0.643137, green: 0.031373, blue: 0.000000, alpha: 1.000000)

var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Center

myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(0,9))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(0,9))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:NSMakeRange(0,9))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(9,8))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(9,8))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(9,8))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:NSMakeRange(9,8))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(17,1))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(18,5))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(18,5))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(18,5))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color4, range:NSMakeRange(18,5))

better answer without the underline nonsense that I added to my previous answer and with natural alignment:

var myString1 = NSMutableAttributedString(string:"hello how are you buddy")

let myString1Font1 = UIFont(name:"Helvetica", size:14.0)

let myString1Color1 = UIColor(red: 0.000000, green: 0.176471, blue: 0.600000, alpha: 1.000000)
let myString1Color2 = UIColor(red: 0.247059, green: 0.411765, blue: 0.117647, alpha: 1.000000)
let myString1Color3 = UIColor(red: 0.607843, green: 0.172549, blue: 0.003922, alpha: 1.000000)

var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Natural

myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color1, range:NSMakeRange(0,9))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(9,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(9,1))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:NSMakeRange(10,7))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(10,7))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(10,7))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(17,1))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:NSMakeRange(18,5))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(18,5))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(18,5))

Oh yes, and I see you want this in a UILabel, well UILabel has a property called "AttributedText", you should set this property to this string I made for you DON'T set the "text" property.

do this like so

myLabel.attributedText = myString1

Also, here's the simplified colors:

let myString1Color1 = UIColor.blueColor()
let myString1Color2 = UIColor.greenColor()
let myString1Color3 = UIColor.redColor()

and even another way to do this:

var myString1 = NSMutableAttributedString(string:"hello how are you buddy?")

let myString1Font1 = UIFont(name:"Helvetica", size:14.0)

let myString1Color1 = UIColor.blueColor()
let myString1Color2 = UIColor.greenColor()
let myString1Color3 = UIColor.redColor()

let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString("hello how ")
let myString1Range2 = originalNSString.rangeOfString("are you")
let myString1Range3 = originalNSString.rangeOfString(" ")
let myString1Range4 = originalNSString.rangeOfString("buddy?")

var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Natural
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.defaultTabInterval = 0
myString1ParaStyle1.firstLineHeadIndent = 0
myString1ParaStyle1.headIndent = 0
myString1ParaStyle1.hyphenationFactor = 0
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1ParaStyle1.lineHeightMultiple = 0
myString1ParaStyle1.lineSpacing = 0
myString1ParaStyle1.maximumLineHeight = 0
myString1ParaStyle1.minimumLineHeight = 0
myString1ParaStyle1.paragraphSpacing = 0
myString1ParaStyle1.paragraphSpacingBefore = 0
myString1ParaStyle1.tailIndent = 0

myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:myString1Range2)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range2)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range2)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range3)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range3)
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:myString1Range4)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range4)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range4)

UILabel text with different font size and color

Suppose you want to have a smaller and gray currency symbol like this:

enter image description here

Just use a NSMutableAttributedString object:

let amountText = NSMutableAttributedString.init(string: "€ 60,00")

// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12),
NSForegroundColorAttributeName: UIColor.grayColor()],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times

// set the attributed string to the UILabel object
myUILabel.attributedText = amountText

Swift 5.3:

let amountText = NSMutableAttributedString.init(string: "€ 60,00")

// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object

// set the attributed string to the UILabel object
myUILabel.attributedText = amountText


Related Topics



Leave a reply



Submit