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];
Related
I have to show multiple line in UILabel (If text is large). Below is my code. I am using separate properties for different iOS versions. Please help me out..
labelLocation.numberOfLines=2;
labelLocation.font=[UIFont systemFontOfSize:25];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
}else{
labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
labelLocation.minimumFontSize=10;
}
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
these two line together works
labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
you can set
yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)
and check if your outlet is set properly.
Try this labelLocation.numberOfLines=0;
I suppose, that your label has to small height. Two lines in systemFontOfSize 25 need height about 60.
If label is to small, system doesn't wrap line.
change ur code to this
labelLocation.numberOfLines=0;
labelLocation.font=[UIFont systemFontOfSize:40];
labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
I would personally recommend you to calculate the height required to show the text and then show it onto the label...never hard code text display components such as UITextView and UILable.
NSString *str = #"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";
`UIFont * myFont = [UIFont fontWithName:#"Arial" size:12];//specify your font details here
//then calculate the required height for the above text.
CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//initialize your label based on the height you got from the above..you can put whatever width you prefer...
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];
myLable.text = str;
myLable.numberOfLines=0;
myLable.font=[UIFont fontWithName:#"Arial" size:12];
//myLable.backgroundColor=[UIColor redColor];
myLable.lineBreakMode = NSLineBreakByWordWrapping;
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.
As I get strings of text down from my database, I need to know how big to make the UITextField and how big to make the Cell of the table that contains the UITextfield.
Are there any clever methods that can determine this? Since it will of course depend on the textfield width and the font used.
Thanks
-Code
NSString has a method sizeWithFont:contrainedToSize: that will give you to the size of a string with a particular font:
UIFont *font = [UIFont systemFontOfSize:12.f];
CGSize size = CGSizeMake(textView.frame.size.width, 1000.f);
CGSize stringSize = [myString sizeWithFont:font constrainedToSize:size];