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
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
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))
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 two different color text
You can't do this within a UILabel
s. 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.
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;
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...
UILabel colored in 2 colors vertically
You can try setting color with image to the label,
Add the following method
func getGradientImage(_ bounds:CGRect) -> UIImage {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor(red: 0.596, green: 0.839, blue: 0.929, alpha: 1.00).cgColor,
UIColor(red: 0.169, green: 0.302, blue: 0.408, alpha: 1.00).cgColor
]
gradientLayer.startPoint = CGPoint.zero
gradientLayer.endPoint = CGPoint(x: 1, y: 1) // changing start and end point value you can set vertical or horizontal
gradientLayer.locations = [0.5,1]
gradientLayer.bounds = bounds
UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, true, 0.0)
let context = UIGraphicsGetCurrentContext()
gradientLayer.render(in: context!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
and set color to your label like
yourLabel.textColor = UIColor(patternImage: getGradientImage(yourLabel.bounds))
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:
Hope this helps.
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)
Related Topics
How to Add Images to the iOS Simulator
Combining 'And' and 'Or' Condition in Nspredicate
Change the Root View of Uihostingcontroller in Swiftui
Invalid Image Path - No Image Found at the Path. Cfbundleicons Xcode 5
Uisearchdisplaycontroller Without Dimming
Exit Application When Click Button - iOS
Swift JSON Error:Could Not Cast Value of Type '_Nsdictionarym' to 'Nsarray'
Autolayout Views Make App Crash on Popviewcontroller
Uilongpressgesturerecognizer Not Calling Its Target Method
Parsing JSON Within JSON in Objective-C
What Are the Differences Between a Uiview and a Calayer
Uitextfield Leftview/Rightview Padding on iOS7
Xcode - How to Drag a Component from One View to Another Without Losing Its Frame