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))
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 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.
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;
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];
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 With Different Colours
You can do with attirbutedSring. Do not forget change font.
let string1 = "Your string";
let string2 = "your string 2";
let attributedString = NSMutableAttributedString(string: "“\(string1)” \(string2)", attributes: [
.font: UIFont(name: "IBMPlexSans", size: 15.0)!,
.foregroundColor: UIColor(red: 128.0 / 255.0, green: 130.0 / 255.0, blue: 135.0 / 255.0, alpha: 1.0),
.kern: 0.4
])
attributedString.addAttribute(.foregroundColor, value: UIColor(red: 1.0 / 255.0, green: 6.0 / 255.0, blue: 16.0 / 255.0, alpha: 1.0), range: NSRange(location: 0, length: string1.count));
yourLabel.attributedText = attributedString;
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))
Related Topics
Tablefooterview Property Doesn't Fix the Footer at the Bottom of the Table View
iOS App Error - Can't Add Self as Subview
Remove Gradient Background from Uiwebview
Custom Init for Uiviewcontroller in Swift with Interface Setup in Storyboard
Uitableview - Change Section Header Color
Autolayout: Removefromsuperview/Removeconstraints Throws Exception and Crashes Hard
Toggling Privacy Settings Will Kill the App
Uigraphicsgetimagefromcurrentimagecontext Memory Leak with Previews
Automatic Preferred Max Layout Width Is Not Available on iOS Versions Prior to 8.0
How to Create IPA in Xcode 6 Without Apple Developer Account
iOS 7 Sprite Kit Freeing Up Memory
Get Size of a View in React Native
iOS Multiple Columns in Uitableview
Location Services Not Working in iOS 11
How to Set the Title of a Uibutton as Left-Aligned
How to Delete All Cookies of Uiwebview
Uipopoverpresentationcontroller on iPhone Doesn't Produce Popover