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 do you have two font colors for a single style using the ::after pseudo-element?
When you can not alter the source for all form fields and append an extra element for this purpose, one chance would be to use ::before
and ::after
and change the display direction for the desired effect.
label.control-label{ display: flex;}
label.control-label::before{ content: ':'; order: 2;}
label.control-label.required::after{ content: '*'; color: red; order: 1;}
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];
How to use multiple font stylings on a single string inside a label?
just required Helvetica
font name with Bold like
lblWithText.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
Just An Example ..
UILable *lbl1 = [[UILable alloc]init];
lbl1.frame = CGRectMake(10,10,100,40);
lbl1.backgroundColor = [UIColor clearColor];
lbl1.textColor = [UIColor blackColor];
lbl1.font = [UIFont fontWithName:@"Helvetica" size:12.0];
lbl1.text = @"Stack";
UILable *lbl2 = [[UILable alloc]init];
lbl2.frame = CGRectMake(110,10,150,40);
lbl2.backgroundColor = [UIColor clearColor];
lbl2.textColor = [UIColor blackColor];
lbl2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
lbl2.text = @"OverFlow";
[self.view addSubview:lbl1];
[self.view addSubview:lbl2];
this is an example which through you get idea ..
UPDATE:
Also See this Example with NSMutableAttributedString:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Blah1:blah-blah%d. Blah2:-%d%%", [currentCoupon.couponPrice intValue],[currentCoupon.couponDiscountPercent intValue]];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,30)];/// Define Range here and also BackGround color which you want
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,30)];/// Define Range here and also TextColor color which you want
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
lblWithText.attributedText = str;
Objective c - Multiple colors in a label
In UILabel basically impossible. If you want to this you must override drawTextInRect
should be executed. But I will recommend OHAttributedLabel. this is have a attributedString is a textcolor can be set to specify a range.
Multiple colors in a C# .NET label
There is no native control in .NET that does this. Your best bet is to write your own UserControl (call it RainbowLabel or something). Normally you would have a custom label control inherit directly from Label, but since you can't get multi-colored text in one label, you would just inherit from UserControl.
For rendering the text, your UserControl could split the text on commas and then dynamically load a differently-colored Label for each chunk. A better way, however, would be to render the text directly onto your UserControl using the DrawString and MeasureString methods in the Graphics namespace.
Writing UserControls in .NET is really not difficult, and this kind of unusual problem is exactly what custom UserControls are for.
Update: here's a simple method you can use for rendering the multi-colored text on a PictureBox:
public void RenderRainbowText(string Text, PictureBox pb)
{
// PictureBox needs an image to draw on
pb.Image = new Bitmap(pb.Width, pb.Height);
using (Graphics g = Graphics.FromImage(pb.Image))
{
// create all-white background for drawing
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0,
pb.Image.Width, pb.Image.Height);
// draw comma-delimited elements in multiple colors
string[] chunks = Text.Split(',');
brush = new SolidBrush(Color.Black);
SolidBrush[] brushes = new SolidBrush[] {
new SolidBrush(Color.Red),
new SolidBrush(Color.Green),
new SolidBrush(Color.Blue),
new SolidBrush(Color.Purple) };
float x = 0;
for (int i = 0; i < chunks.Length; i++)
{
// draw text in whatever color
g.DrawString(chunks[i], pb.Font, brushes[i], x, 0);
// measure text and advance x
x += (g.MeasureString(chunks[i], pb.Font)).Width;
// draw the comma back in, in black
if (i < (chunks.Length - 1))
{
g.DrawString(",", pb.Font, brush, x, 0);
x += (g.MeasureString(",", pb.Font)).Width;
}
}
}
}
Obviously this will break if you have more than 4 comma-delimited elements in your text, but you get the idea. Also, there appears to be a small glitch in MeasureString that makes it return a width that is a couple pixels wider than necessary, so the multi-colored string appears stretched out - you might want to tweak that part.
It should be straightforward to modify this code for a UserControl.
Note: TextRenderer is a better class to use for drawing and measuring strings, since it uses ints. Graphics.DrawString and .MeasureString use floats, so you'll get off-by-a-pixel errors here and there.
Update: Forget about using TextRenderer. It is dog slow.
Have different color and different font size in one label
There are 2 ways to do this
xib file or storyboard
go the label, and choose attribute instead of plain
then do whatever you want there
codes
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"user name is watching walking dead on xxx site"];
[attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
NSFontAttributeName, [UIFont fontWithName:@"WHATEVER FONT" size:FONT_SIZE_HERE],
NSForegroundColorAttributeName, [UIColor blueColor],
nil]
range:NSMakeRange(0, 9)];//9 is the length of "user name"
[attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
NSFontAttributeName, [UIFont fontWithName:@"WHATEVER FONT" size:FONT_SIZE_HERE],
NSForegroundColorAttributeName, [UIColor blueColor],
nil]
range:NSMakeRange(22, 12)];//22 is the start index of "Walking dead"
//and 12 is the length of "Walking dead"
//you got the idea, same way to do the xxx site.
//Check a file called "NSAttributedString.h"
//you will find even more options there
I personally prefer the second solution, since you have more options in the code, and it works for pretty much all the cases. But there is a learning curve.
Hope that helps
Different colors for text on same label using string format
You can use a FormattedText, like this:
The problem with this is that it doesn't support bindings. However, you can set the text from the page code behind. If it doesn't work for you, simply use two labels within a horizontal StackLayout.
UPDATE:
Now spans are bindable! https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text
Related Topics
Xcode Is Looking for Core Data Entity Names with Dot; Not Compiling
Uilabel Layer Cornerradius Negatively Impacting Performance
Invalid Update: Invalid Number of Rows in Section 0
Capturing and Storing a Picture Taken with the Camera into a Local Database/Phonegap/Cordova/Ios
How to Find File Uti for File, Withouth Pathextension, in a Path in Swift
How to Create a Centered Uicollectionview Like in Spotify's Player
(Swift) How to Print "\" Character in a String
Uitableview and Uiview with Keyboardwillshow
What Is 'Vary for Traits' in Xcode 8
iOS 7 -- Navigationcontroller Is Setting the Contentinset and Contentoffset of My Uiscrollview
Is Silent Remote Notifications Possible If User Has Disabled Push for the App
Make a Uibutton Programmatically in Swift