How to append the following characters ‡, †, * as superscript to NSString in iOS - 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:

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 not working: bold the part of NSString

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.

How to use attributed string as navigation bar title in iOS 5.0?

Is there any way to use attributed label on navigation bar on iOS 5? I know it works in iOS 6+ but before that?
I want to use both bold and non bold font there.
You can use TTAttributedLabel to achieve this. Here is the code to do the trick. Suppose you want to set title to "Sample NavBarTitle" where the Sample should bold.
//In your code there may be several ranges, corresponding to how many attributes you want to have
NSRange range = NSMakeRange(0, 6);
//Initialize NSMutableAttributedString
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:#"Sample NavBarTitle"];
//Set the attribute to make "Sample" bold
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Georgia-Bold" size:15] range:range];
//Initialize TTAttributedLabel with rect
TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
//Set the attributedText property of TTAttributedLabel
label.attributedText = string;
//Set navigationItem.titleView to the label view we've created
self.navigationItem.titleView = label;
That's all.
Here is the code that helped me:
// Total string
NSString *tempString = [NSString stringWithFormat:#"%#, %# %# %# %#", gameName, stringConnector, self.bet.gameInfo.draw.drawNumber, stringConnector2, [dateFormatter stringFromDate:self.bet.gameInfo.draw.date]];
NSRange range = [tempString rangeOfString:gameName];
NSMutableAttributedString * string2 = [[NSMutableAttributedString alloc] initWithString:tempString];
// font for all string
UIFont *regularFont = [UIFont fontWithName:BEAU_PRO_LIGHT_FONT_NAME size:21];
CTFontRef font_2 = CTFontCreateWithName((__bridge CFStringRef)regularFont.fontName, regularFont.pointSize, NULL);
[string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_2 range:[tempString rangeOfString:tempString]];
// bold font
UIFont *boldFont = [UIFont fontWithName:BEAU_PRO_SEMIBOLD_FONT_NAME size:21];
CTFontRef font_1 = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
[string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_1 range:range];
TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 600, 20)];
label.attributedText = string2;
[label sizeToFit];
self.navigationItem.titleView = label;

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 can I underline text in iOS 6?

I am trying to underline some text in a label. However, I do't know how to get the range of the entire text in the label. This is what I have so far:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)} range://??];
self.tableLabel.attributedText = mat;
What should I put for the range?
For the range you may want to use:
NSMakeRange (0, mat.length);
Like this:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:#(NSUnderlineStyleSingle)
range:[strComplete rangeOfString:strFirst]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[strComplete rangeOfString:strSecond]];
cell.textLabel.attributedText = attributedString;

Resources