Change font of separate strings detailTextLabel - ios

How can I change the font of one string in the detailTextLabel of my UITableViewCell?
NSString *detailStr = [NSString stringWithFormat:#"%# %#",post.score,post.domain];
cell.detailTextLabel.text = detailStr;
I basically want the post.score string to be one color and the post.domain string to be another.

Answer : NSAttributedString.
Try this :
int count1 = [post.score length];
int count2 = [post.domain length];
NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:#"%# %#",post.score,post.domain];
[textLabelStr setAttributes:#{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(0, count1)];
[textLabelStr setAttributes:#{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(count1 + 1, count2)];
cell.detailTextLabel.attributedText = textLabelStr;
Note : Not tested, but Write the Code to help you.

If you're happy to use iOS6+ APIs then check out NSAttributedString and use cell.detailTextLabel.attributedText = ...

I think your looking for the setAttributedText: method of UILabel. You can pass an NSAttributedStringto it in order to stylize different portions of the string in different ways.
I wrote a sample below that should do what you're asking for.
NSAttributedString *scoreAttributedString = [[NSAttributedString alloc] initWithString:[post score] attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
NSAttributedString *domainAttributedString = [[NSAttributedString alloc] initWithString:[post domain] attributes:#{NSForegroundColorAttributeName: [UIColor greenColor]}];
NSMutableAttributedString *detailMutableAttributedString = [[NSMutableAttributedString alloc] init];
[detailMutableAttributedString appendAttributedString:scoreAttributedString];
[detailMutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:#" "]];
[detailMutableAttributedString appendAttributedString:domainAttributedString];
[[cell detailTextLabel] setAttributedText:detailMutableAttributedString];

Related

Bolding an nsstring

I have looked online for ways to bold a NSString and either it's not there or I can't seem to find it. I simply want to bold the word in the NSString but so far, what I have done is not woking:
NSString *player1Name = [[UIFont boldSystemFontOfSize:12] fontName];
I have relied on this to make "player1Name" bold but it doesn't work at all.
When I run it I get this:
Thanks in advance to anyone who helps me figure out the solution.
You can not bold a NSString... Try a NSAttributedString...
NSAttributedString *player1Name = [[NSAttributedString alloc] initWithString:#"name" attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]}];
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/
fontName is a property on UIFont
[[UIFont boldSystemFontOfSize:12] fontName]
returns the font name HelveticaNeueInterface-MediumP4
Try this:
NSString *myString = #"My string!";
NSAttributedString *myBoldString = [[NSAttributedString alloc] initWithString:myString
attributes:#{ NSFontAttributeName: [UIFont boldSystemFontOfSize:35.0] }];
Also you can set range for particular text like:
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(2, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
You can't make NSString bold. Instead of that you can make UILabel text into bold. Because UILabels are for UI representation & NSStrings are just objects. So make your UILabel bold
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

NSMutableAttributedString doesn't show string in different color for some specific range in iOS 8 & iOS 9

I am using attributes string in my code. To set font & color of text for some specific range.
For this I am using below code.
Font is working for range but color is not set for that range. There may be some issue in NSForegroundColorAttributeName.
Please let me know if anyone have solution.
NSDictionary *attrs = #{ NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor redColor] };
NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc]initWithString:#"New Message from Test"];
[titleStr addAttributes:attrs range:NSMakeRange(0, 3)];
I also faced the similar problem. So I passed my string as HTML string and got it done like this.
NSString *name = #"<center><font color='#cd1c5f' size='4px'>"
#"New"
#"</font>"
#"<font color='#000000' size='4px'>"
#"Message from test"
#"</font></center>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[name dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
_lbl_nm.attributedText=attrStr;
You can append two attribute string with different attributes of red colour & text colour. Lets try using the following code chunk.
NSDictionary *attrs = #{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
NSMutableAttributedString *titleString = [[[NSMutableAttributedString alloc] initWithString:#"New" attributes:attrDict] autorelease];
attrs = #{NSForegroundColorAttributeName:[UIColor lightTextColor], NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
NSAttributedString *messageString = [[[NSAttributedString alloc] initWithString:#" Message from Test" attributes:attrs] autorelease];
[titleString appendAttributedString:messageString];
[self.titleTextView setAttributedText:titleString];
Check if you are changing the textcolor of the label after you set the attributed title. Thats the only way you will face this problem. Do it before you set the title.
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.attributedText = titleStr;
I don't see any problems with your code. I just copied your code and tested, everything's fine.

iOS - UITableViewCell make text bold

I have a string:
NSString *userInfo = #"James Johnson #james";
What i want to do is bold James Johnson and keep #james normal font.
So what I have tried is using NSAttributedString but somewhere I'm doing something wrong in order to complete the process.
This is what I have tried:
NSString *user = #"James Johnson #james";
UIFont *fontLight = [UIFont fontWithName:#"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:userInfo];
//TESTING WITH RANDOM PARTS OF THE STRIN
[string addAttribute:NSForegroundColorAttributeName value:fontLight range:NSMakeRange(0, 3)];
[string addAttribute:NSForegroundColorAttributeName value:fontBold range:NSMakeRange(3, 5)];
NSString *str = [string string];
cell.textLabel.text = str;
Is there a way I can make this work even if I'm on the wrong direction?
What's not working
For some reason, the characters from range 0 - 3 is not being a light font...instead the entire cell.textLabel.text is bold somehow and is not font size 14 which i had specified in the UIFont.
Your last part of it is wrong. You must create your NSAttributedString and finally trash the formatting by using
NSString *str = [string string];
As NSString doesn't know anything about formatting you have to use the NSAttributedString to assign it to the cell's textLabel:
cell.textLabel.attributedText = string;
You should set attributedString to attributed text
Comment these lines
//NSString *str = [string string];
//cell.textLabel.text = str;
And write this
cell.textLabel.attributedText = string;//Set NSMutableAttributedString only not NSString
UILabel has a property attributedText. You should be assigning this property with your attributed string and not use text property.
Make changes in you code as follows :
You will require to report all the characters with in the NSMutableAttributedString, so specify them with in the range, while adding an attribute.
Provide correct attribute name, else you will have an exception here, in this case you should use "NSFontAttribteName" in place of "NSForegroundColorAttributeName".
NSString *user = #"James Johnson #james";
UIFont *fontLight = [UIFont fontWithName:#"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:user];
[string addAttribute:NSFontAttributeName value:fontLight range:NSMakeRange(0, 20)];
[string addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(0, 5)];
cell.textLabel.attributedText = string;

Put 2 NSAttributedStrings in one Label

is it possible to put 2 NSAttributedStrings in only one label?
For example:
NSString *a = [car valueForKey:#"name"];
NSString *b = [car valueForKey:#"version"];
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:a attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : #1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
NSAttributedString *title2;
title2 = [[NSAttributedString alloc] initWithString:b attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : #0 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //2
cell.textLabel.attributedText = //what should I write here?
You can use the -appendAttributedString:(NSString *) method to append one attributed string to the other. Since you can make both of your attributed strings mutable, you can also include separators (semicolons, commas) to differentiate between your two different strings in the one label.
Here is some sample code:
NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:#"Hello"];
NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:#"Hello 2"];
NSMutableAttributedString *semiStr = [[NSMutableAttributedString alloc] initWithString:#" ; "];
[string1 appendAttributedString:semiStr]; //separate string 1 and 2 by semi-colon
[string1 appendAttributedString:string2];
textLabel.text = string1; //which string2 is now appended to
Obviously, in your attributed strings, you would have attributes (like you stated in your question).
You can also always trust Apple's documentation on the NSMutableAttributedString class to find suitable methods to use next time.
You can use NSMutableAttributedString to either append the two attributed strings together or to build the attributed string and set the attributes at specified ranges (based on the source strings).

Increasing the font size of first letter in UILabel.text

I would like the first letter of the UILabel to have a different font size from others, a few font size larger. Need some guidance on how to do it. The label has a lot of words.
This is what I have tried:
NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstLetter = [[words objectAtIndex:0] substringToIndex:1];
But got stuck in increasing the size of the NSString. Is there a better way? I am welcome to suggestions and guidance.. Thanks..
EDIT:
[Label setFont:[UIFont fontWithName:#"Arial" size:12.f]];
NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
UIFont *fontFirst=[UIFont fontWithName:#"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *finalString=[[NSAttributedString alloc] initWithString:[[words objectAtIndex:0] substringToIndex:1] attributes:attrsDictFirst];
To get, for example, this:
Say this:
NSString* s2 = #"Fourscore and seven years ago, our fathers brought forth "
#"upon this continent a new nation, conceived in liberty and dedicated "
#"to the proposition that all men are created equal.";
NSMutableAttributedString* content2 =
[[NSMutableAttributedString alloc]
initWithString:s2
attributes:
#{
NSFontAttributeName:
[UIFont fontWithName:#"HoeflerText-Black" size:16]
}];
[content2 setAttributes:
#{
NSFontAttributeName:[UIFont fontWithName:#"HoeflerText-Black" size:24]
} range:NSMakeRange(0,1)];
[content2 addAttributes:
#{
NSKernAttributeName:#-4
} range:NSMakeRange(0,1)];
NSMutableParagraphStyle* para2 = [NSMutableParagraphStyle new];
para2.headIndent = 10;
para2.firstLineHeadIndent = 10;
para2.tailIndent = -10;
para2.lineBreakMode = NSLineBreakByWordWrapping;
para2.alignment = NSTextAlignmentJustified;
para2.lineHeightMultiple = 1.2;
para2.hyphenationFactor = 1.0;
[content2 addAttribute:NSParagraphStyleAttributeName
value:para2 range:NSMakeRange(0,1)];
Then assign content2 to a label's attributedText.
Need to use NSAttributedString. iOS6.0 onwards
After getting the first letter make put it into attributed string, change its size.
take rest of the string set other size.
UIFont *font=[UIFont fontWithName:#"Arial" size:12.f];
NSDictionary *attrsDict=[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attribString=[[NSAttributedString alloc] initWithString:[words[0] substringFromIndex:1] attributes:attrsDict];
UIFont *fontFirst=[UIFont fontWithName:#"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *firstString=[[NSAttributedString alloc] initWithString:[attribString subStringToIndex:1] attributes:attrsDictFirst];
[attribString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstString];

Resources