I have a UILabel in which I need to display two different colored strings:
Below is my code:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: lbl_question.attributedText];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:52.0f/255.0f green:104.0f/255.0f blue:165.0f/255.0f alpha:1.0f] range: NSMakeRange(0,[result integerValue]+1)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:75.0f/255.0f green:75.0f/255.0f blue:75.0f/255.0f alpha:2.0f] range: NSMakeRange([result integerValue]+1,[strq length])];
[lbl_question setAttributedText: text];
In iOS 6 it works fine but in ios 5 and earlier versions the two strings just got ovelapped everytime.
also i want to get width according to text and font. increase height according to their text.
I am sure there has to be solution...Please help me out with this....
You can do this using NSMutableAttributedString. This will work in ios 6,ios 7 and ios 8 also.
Note that NSMakeRange(startin gpoint counting from zero,number of character);
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:#"RedGreenBlue"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(8,4)];
lable.attributedText=string;
Thanks
From the NSAttributedString UIKit Additions Reference:
NSForegroundColorAttributeName The value of this attribute is a
UIColor object. Use this attribute to specify the color of the text
during rendering. If you do not specify this attribute, the text is
rendered in black. Available in iOS 6.0 and later.
You need two labels, or use something like TTTAttributedLabel or DTCoreText.
Try TTTAttributedLabel. It's a subclass of UILabel that supports NSAttributedStrings, which would make it easy to have multiple colors, fonts, and styles in the same string.
Related
I have a UITextView and a bar with three buttons which toggle bold, italic and underline.
I've set the TextView's text to attributed and it's "Allow Editing Attributes" option to YES.
Although the code for editing a selected text is working (here's an example):
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20];
NSDictionary *dict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
//edit selection
NSRange selectedRange = _noteTextView.selectedRange;
[[_noteTextView textStorage] beginEditing];
[[_noteTextView textStorage] addAttribute:NSFontAttributeName value:font range:selectedRange];
[[_noteTextView textStorage] addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:selectedRange];
[[_noteTextView textStorage] endEditing];
[[_noteTextView textStorage] setAttributes:dict range:selectedRange];
I am having problems with setting the attributes at my textview's indicator.
[[_noteTextView textStorage] addAttribute:] is not working or I might have a problem with the range.
Any advice?
Thanks in advance!
Try using this to set the range [textView rangeForUserParagraphAttributeChange];
It will set the attributes for the whole paragraph not just the selected range.
EDIT:
If there is text beyond the caret position you may have to extend the range by one character in order to ensure that subsequent characters that are typed in will fall within the range the attributes were applied to.
Applying attributes to a range does just that, it applies them to the range not to any text typed in beyond the range. If the length of the range is ZERO (i.e. nothing selected) then my guess is your attributes get applied to nothing.
For the first time i decided to use NSAttributed string for my UILabel,
nevertheless when i set my UILabel attributes (font, color, alignement) of my labels in IB (interface builder interface) when i run my code the attributes are never respected ! I have to manually add attributes to my labels :
_myLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:25];
Can someone help me ?
What you're doing is applying a font font the text property of the label. If you'd like to assign a font for attributed text, you have to assign that font as one of the attributed string's attributes.
NSString *inputString = #"fsdfdsf";
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"Arial-BoldMT" size:25]};
NSAttributedString *attributecString = [[NSAttributedString alloc] initWithString:inputString attributes:attributes];
[_myLabel setAttributedText:attributecString];
I want to create a label that contain text that have to different color. Like this,
I have done this in iOS 6 using NSMutableAttributedString
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: self.exerciseLbl.attributedText];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:(187/255.0) green:(57/255.0) blue:(38/255.0) alpha:1] range: NSMakeRange(0, 2)];
[self.exerciseLbl setAttributedText: text];
But this will not work for iOS 5.1 and below. So how could I achieve same result in iOS 5.
/**(1)** Build the NSAttributedString *******/
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:#"Hello World!"];
// for those calls we don't specify a range so it affects the whole string
[attrStr setFont:[UIFont systemFontOfSize:12]];
[attrStr setTextColor:[UIColor grayColor]];
// now we only change the color of "Hello"
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)];
/**(2)** Affect the NSAttributedString to the OHAttributedLabel *******/
myAttributedLabel.attributedText = attrStr;`
// Use the "Justified" alignment
`myAttributedLabel.textAlignment = UITextAlignmentJustify;`
Since iOS 6, UIKit supports drawing attributed strings, so no extension or replacement is needed.
From UILabel:
#property(nonatomic, copy) NSAttributedString *attributedText;
You just need to build up your NSAttributedString. There are basically two ways:
Append chunks of text with the same attributes - for each part create one NSAttributedString instance and append them to one NSMutableAttributedString
Create attributed text from plain string and then add attributed for given ranges – find the range of your number (or whatever) and apply different color attribute on that.
For iOS lower than 6, you can go ahead with https://github.com/AliSoftware/OHAttributedLabel. It provides category and subclassing which worked for me..I hope will be helpful to you too.
I am trying to create a UILabel or UITextView with bold and normal text inside.
I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.
I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.
Can anyone give some sort of solution to this?
Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:
just #import "NSAttributedString+Attributes.h"
and then implement it like this:
NSString *string1 = #"Hi";
NSString *string2 = #"How are you ?";
NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];
[attr1 setFont:[UIFont systemFontOfSize:20]];
NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]
[attr2 setFont:[UIFont boldSystemFontOfSize:20]];
[attr1 appendAttributedString:attr2]
CATextLayer *textLayer = [CATextLayer layer];
layer.string = attr1;
layer.contentsScale = [[UIScreen mainScreen] scale];
(Your_text_label).layer = textLayer;
OR (if you want to render on a view directly)
[(Your_View_Name).layer addSublayer:textLayer];
Up until iOS 6.0, you couldn't do this with a normal UILabel or UITextView, but you can use NSAttributedString objects with a few possible open source solutions.
Like TTAttributedLabel or OHAttributedLabel.
One solution built into the iOS SDK, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString.
And, like the commenters below say, yes you can do this with the "attributedText" property. Horray! (for Apple listening to developer's very often repeated feature requests)
I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!
Here are the steps:
Place your textView at the desired location
Select the textView and open up the Attributes tab under the Utilities panel
Change the textView text to "attributed"
Enter your desired text
Now, highlight whatever text you want bolded, underlined, etc.
Click on the "T" button next to the fontName
In the popup, select your desired typeface (ex: Bold)
You should see the desired typeface displayed in the Utilities panel
Enjoy!
The following code is for iOS 6.0 and above. The result is that the text "This is bold" will be in bold and "This is not bold." will be normal text.
if ([self.registrationLabel respondsToSelector:#selector(setAttributedText:)])
{
// iOS6 and above : Use NSAttributedStrings
const CGFloat fontSize = 17;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
//UIColor *foregroundColor = [UIColor clearColor];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded
// Create the attributed string (text + attributes)
NSString *text = #"This is bold and this is not bold.;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:subAttrs];
[attributedText setAttributes:attrs range:range];
// Set it in our UILabel and we are done!
[self.registrationLabel setAttributedText:attributedText];
}
If you have an issue with editing the attributed text in the inspector, copy/paste the text into a rich text editor, adjust it, switch the textView Text options to Attributed and paste. Vwala.
I have a UILabel which has text containing both chinese and english characters,
now I want to set a font for chinese and another font for english,
how to do this?
There are couple of things that might be of interesting to you:
OHAttributedLabel
and TTTAttributedLabel
OHAttributedLabel stays it is capable of dealing with mixed fonts, color, size, ...
Generally one label can have only one font. Still if you want to show different font for different languages than you can keep different language string in different labels and arrange them the way you want.
See this to decide size of your labels.
Resize UITableViewCell to UILabel's height dynamically
and this is also helpful.
How do I wrap text in a UITableViewCell without a custom cell
I do not believe this is possible. The font property set in a UILabel would apply to the entire string specified in the text property of that UILabel.
I've not tried using chinese font, but you can use the following code to set different / multiple fonts & other properties on Label using NSMutableAttributedString. Foll is my code:
UIFont *ArialFont = [UIFont fontWithName:#"arial" size:18.0];
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
UIFont *VerdanaFont = [UIFont fontWithName:#"verdana" size:12.0];
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[AattrString appendAttributedString:VattrString];
lblText.attributedText = AattrString;
Note that lblText is the UILabel, outlet as file owner.
One can keep on appending as many NSMutableAttributedString he wants..
Also Note that I've added verdana & arial font in my project & added a plist for the same.