NSMutableAttributedString not working: bold the part of NSString - ios

Trying to bold the starting part of NSString. Using the code mentioned below.
-(void)setText {
NSString *strEmail = #"Email: HR_Contact#sre.com";
NSMutableAttributedString *attributedEmail = [[NSMutableAttributedString alloc]initWithString:strEmail];
NSString *boldFontName = [[UIFont fontWithName:_fontMyriadBold size:20] fontName];
NSRange boldedRange = NSMakeRange(0, 5);
[attributedEmail beginEditing];
[attributedEmail addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attributedEmail endEditing];
lblEmailAddress.attributedText = attributedEmail;
}
It doesn't make any change. Why so, what is the issue am not getting no warnings or errors.
Please guide.
Thanks in advance.

Check this.
NSString * strEmail = #"Email: HR_Contact#sre.com";
NSMutableAttributedString * attributedEmail = [[NSMutableAttributedString alloc] initWithAttributedString:strEmail];
NSRange boldedRange = NSMakeRange(0, 5);
[attributedEmail addAttribute: NSFontAttributeName value:[UIFont fontWithName:_fontMyriadBold size:20] range:boldedRange];
[attributedEmail addAttribute: NSForegroundColorAttributeName value: [*UICOLOR*] range:boldedRange]; // if needed
[lblEmailAddress setAttributedText: attributedEmail];

Where are you setting this attributed string? Maybe in one of the -init? Maybe lblEmailAddress is still nil at this point where are you trying to set it. If so, that is the reason why it doesn't work for you. If I understood correctly lblEmailAddress comes from *.xib.

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

How to change properties of particular text in a label

I have a label and i set the text of the label programmatically. I want to set one of the word to be bold and the rest normal. However, i am unable to control the properties of the text. For example, I want this "This is an example" but am only able to achieve this "This is an example".
Try this:
NSString *text = #"This is an example";
NSString *textBold = #"example";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString beginEditing];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:20.0f]
range:[text rangeOfString:textBold]];
[attributedString endEditing];
[labelObj setAttributedText:attributedString];
Take a look at the attributedText property of the label. It lets you assign styled text using an NSAttributedString. Explaining how to build an NSAttributedString is beyond the scope of an SO answer, but you should be able to find ample information both in the Xcode help system and online.
Let me show you a demo about the attributedText.
NSDictionary*subStrAttribute1 = #{
NSForegroundColorAttributeName: [UIColor redColor],
NSStrikethroughStyleAttributeName:#2
};
NSDictionary *subStrAttribute2 =#{
NSForegroundColorAttributeName: [UIColor greenColor]
};
NSString *strDisplayText3 =#"Red and Green";
NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc] initWithString:strDisplayText3];
[attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0,3)];
[attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8,5)];
self.lblInfo3.attributedText= attributedText3;
Since ios6 uilabel supports attributed strings, So you can use it.
For your particular case below code will work-
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"This is an example"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(11, 7)];
label.attributedText = string;

How can I scan a string for numbers to adjust the font size in an iOS app?

I'm off to a good start. But I would like to make this code more dynamic.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont systemFontOfSize:10.0f];
UIFont *smallFont = [UIFont systemFontOfSize:9.0f];
[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];
Say I have the following String:
#"C3OC2OH4"
I would like to adjust the font size of the numbers only to fulfill a chemistry application. How can I scan the above string, and create a series of custom ranges to plug into my function above, which adjusts the font size?
You can enumerate the characters and apply the attributes like this.
NSString * string = #"C3OC2OH4";
NSMutableAttributedString * attributedString = [NSMutableAttributedString new];
NSDictionary * subscriptAttributes = #{NSFontAttributeName : [UIFont systemFontOfSize:10.0],
NSBaselineOffsetAttributeName : #(-5.0)};
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSCharacterSet * letterCharacterSet = [NSCharacterSet letterCharacterSet];
if ([substring rangeOfCharacterFromSet:letterCharacterSet].location != NSNotFound)
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring]];
else
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring attributes:subscriptAttributes]];
}];
In this case substring is each character and is appended without any change if it is a letter. If it is a number we change the font size and shift the baseline. Change them according to your need.
Good luck.

How to show multiple line text that has different font without using multiple Labels

I have to show a text paragraph that contains few words in bold font. ex.
If I use different labels then on changing the orientation it does not resize properly.
Can anyone tell me what can the best way to do it.
You can use an UITextView using NSAttributedString (have a look to the apple doc)
And you have an explanation of how to use it here.
You can find the range of your word and change the font or the color or whatever using :
- (IBAction)colorWord:(id)sender {
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
NSArray *words = [self.text.text componentsSeparatedByString:#" "];
for (NSString *word in words)
{
if ([word hasPrefix:#"#"])
{
NSRange range=[self.text.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}
}
[self.text setAttributedText:string];
}
You have to use a NSAttributedString and assign it to the UITextField, this is an example:
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:yourString];
[myAttributedString addAttribute:NSFontAttributeName
value:boldFont
range:NSMakeRange(0, 2)];
[myAttributedString addAttribute:NSFontAttributeName
value:regularFont
range:NSMakeRange(3, 5)];
[self.description setAttributedText:myAttributedString];
Find all the doc here:
NSAttributedString
For your case, you can use a webView and load it with your string:
[webView loadHTMLString:[NSString stringWithFormat:#"<html><body style=\"background-color: transparent;\">This is <b><i>Test</b></i> dummy text ..</body></html>"] baseURL:nil];

How to append the following characters ‡, †, * as superscript to NSString in iOS

I need append the following characters ‡, †, * as superscript to NSString in iOS . Need your help. I use the following http://en.wikipedia.org/wiki/General_Punctuation_(Unicode_block) link but they are appending to NSString , But i want them as superscript
Try to use this one. And you need to #import <CoreText/CTStringAttributes.h>. This code works only in iOS6 or later version.
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 200, 40)];
NSString *infoString=#"X2 and H20 A‡ B† C*";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(1, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#-1 range:NSMakeRange(8, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(12, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(15, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(18, 1)];
lbl.attributedText = attString;
[self.view addSubview:lbl];
Output
I hope this will help you
NSString does not allow you to format specific parts of the text. If you're planning to display your text in a UILabel, UITextField, or UITextView (and your app doesn't have to run on anything below iOS 6), you can use NSAttributedString and apply a kCTSuperscriptAttributeName attribute with the value #1. If you're using a UIWebView, use HTML's <sup> element.
This is how you could achieve that:
NSString *string = #"abcdefghi";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSInteger num1 = 1;
CFNumberRef num2 = CFNumberCreate(NULL, kCFNumberNSIntegerType, &num1);
[attrString addAttribute:(id)kCTSuperscriptAttributeName value:(id)CFBridgingRelease(num2) range:NSMakeRange(6, 3)];
self.label.attributedText = attrString;
, where label is a UILabel property already added to the UI.
Make sure you add first the CoreText framework, also add this line on top of you .m file.
Hope it helps you somehow
Swift
Step 1.
import CoreText
Step 2.
let range1 = NSMakeRange(1, 1)
let range2 = NSMakeRange(5, 1)
let mutableStr : NSMutableAttributedString = NSMutableAttributedString(string: "h20 x2")
mutableStr.addAttribute(kCTSuperscriptAttributeName as String, value:-1, range: range1)
mutableStr.addAttribute(kCTSuperscriptAttributeName as String, value:1, range: range2)
self.lbl.attributedText = mutableStr
Output:

Resources