Put 2 NSAttributedStrings in one Label - ios

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).

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.

String and attributions problems

Ive been messing around with this without proper results. My code is as follows
NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc]initWithString:((User *)appDelegate.users[self.currentUserIndex]).name];
[nameString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:NSMakeRange(0, nameString.length)];
NSString *adviceString = [[NSString alloc] initWithFormat:#"%#, remember be extra aware of the \n cat today. The dog index is 4, dogcatcher \n suggests you should at minimum wear \n a dog and apply cat...", [nameString string]];
So, it doesn't bold the username as desired. I tried using NSAttributedString for adviceString but then I can't use the initWithFormat: method and list the variables after the string, and I don't see any NSAttributedString equivalent. I wanted to you NSAttributedString instead of NSMutableAttributedString, but it doesn't seem to recognise the addAttribute:value:fontWithName:range method. Can anybody help me out? Any help much appreciated.
That's for sure, you need to use NSAttributedString also for adviceString to get attributed.
The code goes like this, if you need to use stringWithFormat:
NSString *fullName = #"Anoop Kumar Vaidya";
NSMutableAttributedString *attributedName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"Hello %#", fullName]
attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]}];
In the attributes you can add few more desired/required attributes.
EDIT 2:
You may like to use some methods as :
-(NSMutableAttributedString *)normalString:(NSString *)string{
return [[NSMutableAttributedString alloc] initWithString:string
attributes:#{}];
}
-(NSMutableAttributedString *)boldString:(NSString *)string{
return [[NSMutableAttributedString alloc] initWithString:string
attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]
}];
}
And then use them as per your requirement:
NSMutableAttributedString *attributedName = [self boldString:fullName];
[attributedName appendAttributedString:[self normalString:#" is creating one"]];
[attributedName appendAttributedString:[self boldString:#" bold"]];
[attributedName appendAttributedString:[self normalString:#" string."]];

Why does this NSMutableAttributedString addAttribute work only if I use a mutableCopy

I have the following code :
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:#"Son"];
[boldS addAttribute:NSFontAttributeName value:SOMEBOLDFONT range:NSMakeRange(0, boldS.length)];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
As you can see, I want to bold the Son part. This does not work if I do the above statements but only works if I do :
[[attrS mutableCopy] replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
What might be the reason for that?
addAttribute works regardless of whether you take a mutableCopy. Your question is based on a false assumption. It therefore has no answer.
Run this:
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:#"Son"];
UIFont *someBoldFont = [UIFont fontWithName:#"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];
NSMutableAttributedString *attrSCopy = [attrS mutableCopy];
[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
withAttributedString:boldS];
NSLog(#"%#", [attrS isEqual:attrSCopy] ? #"equal" : #"different");
It will output equal. Comment out the replaceCharactersInRange: for either attrS or attrSCopy and it will output different.

Change font of separate strings detailTextLabel

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];

Resources