How to make bold font NSString value? - ios

In my iOS application, how can I make bold font in NSString value? I used below code for making bold font text in NSString value, but showing error
like this:
Undefined symbols for architecture i386:
"_kCIAttributeName",
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:#"%#%#%#",key,#":",#" "];
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCIAttributeName value:boldFontName range:boldedRange];
[attrString endEditing];
NSString *string = [NSString stringWithFormat:#"%#%#",attrString,[dic objectForKey:key]];
How do I make bold font in string?

the NSString is for store a simple texts without any font formatting.
the UILabel has a font property, you can use it to set the visible text's font on the UI, but the UILabel can work with one format only and the whole text gets the same font and style.
if you really want to use a kind of rich text document, you should create a UIWebView class and you can show any document inside with the standard HTML tags.
UPDATE
the UILabel can store attributed string since iOS6, which gives you a chance to show rich-formatted text in one single UILabel instance, even using various fonts or sizes, etc...

Related

How to create Hyperlink on particular textView text in ios

NSString *str = #"We’ll notify you before we make changes to these
terms and give you the opportunity to review and comment on the revised
terms before continuing to use. If you have any question just email
me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString
alloc]initWithString:str attributes:nil];
[str rangeOfString:#"email"]];
[self.textView setAttributedText:aStr];
this is my text i want create link on email.
you can do something like,
NSString *str = #"We’ll notify you before we make changes to these terms and give you the opportunity to review and comment on the revised terms before continuing to use. If you have any question just email me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:#"Your Link here" range:[str rangeOfString:#"email"]];
[self.textView setAttributedText:aStr];
Update
You can add font attribute to make it bold. For example if you want to make We’ll notify bold then
[aStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14.0] range:[str rangeOfString:#"We’ll notify"]];
add this line before you set attributed text to your textView !
Add this property to your textview
textview.dataDetectorTypes = UIDataDetectorTypeLink;

How to BOLD certain words within an NSString paragraph

I was wondering if it was possible to bold specific words within a text that is being held in an NSString.
I am able to bold, change the font of characters based on their location within the string using NSMutableAttributedString like this:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:utf8String];
UIFont *boldFont = [UIFont boldSystemFontOfSize:18];
NSRange boldedRange = NSMakeRange(0, 9);
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFont
range:boldedRange];
[attrString endEditing];
But this changes the font of the first 9 characters in the String.
What I want is to put in Bold the following words should they be found within the string: "Questions", "Did you know" and "Further reading". Is this possible? The fact is that I don't know their position in the string.
I did have a look at the question suggesting this is a duplicate, but my question is not exactly the same and the answers provided did not help. I Need to find ranges within a string and then add them to an NSMutableAttributedString, and this is the bit I am asking help for. An answer has been provided that explains how to do that.
EDIT:
The supposed duplicate and its answer DO NOT answer the question. This question is more than just finding specific words within a paragraph, it is also about how to format them using NSMutableAttributedString. The answer provided below is the answer.
Thanks!
If you don't know substring position then you can use NSRange to find position of substring and using position you can make changes to the substring using NSMutableAttributedString class.
example :
UIFont *fontForlabel1 = [UIFont fontWithName:#"Helvetica-Bold" size:14.0];
UIFont *fontForlabel2 = [UIFont fontWithName:#"Helvetica-Bold" size:19.0];
UIFont *fontForlabel3 = [UIFont fontWithName:#"Helvetica" size:12.0];
NSString *text = #"Do you know that or did you know that?";
NSRange range;
UILabel *setlable;
NSMutableAttributedString * attributedString= [[NSMutableAttributedString alloc] initWithString:text];
range = [text rangeOfString:#"Do you know"];
[attributedString addAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:fontForlabel1} range:range];
range = [text rangeOfString:[#"or"];
[attributedString addAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:fontForlabel2} range:range];
range = [text rangeOfString:#"did you know that"];
[attributedString addAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:fontForlabel3} range:range];
setlable.attributedText=attributedString;

"NSAttributedString initWithData: options: documentAttribute: error:" not working as expected

I'm trying to fill a UITableView with HTML strings. Getting the strings, putting them into cell etc. everything is ok. And I want to change my NSAttributedText's font and font size. I wrote the code below.
I checked the code via the first NSLog, if my UIFont is in the dictionary as expected; so it is in the dictionary. still no problem. but my font and font size is not changing on runtime. There is no console error. Just not working.
Second NSLog is for checking the attributedString's font and size. It says, my string is "Times New Roman" with fontSize:12. But as you will see in the code, I'm trying to make it "Verdana" with Size 15
Can anyone help me about this? Thanks.
PS: I'm keeping the strings in some objects. obj.name is the string which I want to write into table cell.
Html strings have some unwanted tags, so the function [self clearTheHtml:obj.name] function is clearing them.
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
UIFont *font = [UIFont fontWithName:#"Verdana" size:15];
[attributes setObject:font forKey:NSFontAttributeName];
NSString *s = [self clearTheHtml:obj.name];
NSLog(#"%#", [attributes objectForKey:NSFontAttributeName]); //to see if my font is not in the dictionary.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[s dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:&attributes error:nil];
NSLog(#"%#", attributedString);
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
cell.textLabel.attributedText = attributedString;
EDIT:
here is the NSString *s :
2014-01-25 07:38:04.527 KadirB[7483:70b]
''Narakas 2013 Brachial Plexus Surgery Symposıum'' 23-25 Mayıs 2013 tarihleri arasında Montreux, İsviçre de yapıldı. Brakial Plexus sorunlarına yönelik son gelişmeler toplantıda tartışıldı.
I've had the same problem just a couple of days ago. I solved by simply changing the implementation like so:
...
NSMutableAttributedString *attributedString = ...;
[attributedString addAttributes:#{NSFontAttributeName: font} range:NSMakeRange(0, attributedString.length)];
I hope it helps you as well.

UITextView customization

I want to format the test in my textview, some texts in bold some thing in italic like that. Is it possible for uitextview?
Right now I'm using webview with HTML strings.
eg:
<html><head><style type=\"text/css\">h3 {color:white;} p {color:pink;} p {text-align: center} p {font-family:helvetica;font-size:20px;}</style></head><body>\
<h3></h3>\
<p><b>some text </b></p>\
<p>Short some text</p>\
<p>Child Infusion 7.5 to 15 mg/kg/hr<br>ie 7.5 to 15 times weight per hour</p>\
<p>Adult Infusion 3 to 12 mg/kg/hr<br>ie 3 to 12 mg times weight per hour</p>\
</body></html>
You can use NSAttributedString, Set Text Font, Foreground And Background Colors, StrikeThrough And Shadow etc..
Attributed strings make an association between characters and their attributes. Like NSString objects, there are two variations, NSAttributedString and NSMutableAttributedString.
Although previous versions of iOS supported attributed strings, it wasn’t until iOS 6 that controls such as buttons, labels, textfields and textviews defined a property to manage attributes.
Attributes are applied to a range of characters, so you can for example, set a strikethrough attribute for just a portion of a string. It’s also important to note that the default font for attributed string objects is Helvetica 12-point. Keep this in mind if you set the font attribute for a range other than the complete string.
The following attributes can be set with attributed strings:
NSString *const NSFontAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSUnderlineStyleAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;
here are some examples
//-----------------------------
// Create attributed string
//-----------------------------
NSString *str = #"example for underline \nexample for font \nexample for bold \nexample for italics";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
// Add attribute NSUnderlineStyleAttributeName
//[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(12, 9)];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(12, 9)];
// Set background color for entire range
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor yellowColor]
range:NSMakeRange(0, [attributedString length])];
// Create NSMutableParagraphStyle object
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
// Add attribute NSParagraphStyleAttributeName
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [attributedString length])];
// Set font, notice the range is for the whole string
UIFont *font = [UIFont fontWithName:#"Helvetica" size:18];
[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(35, 4)];
// Set font, notice the range is for the whole string
UIFont *fontBold = [UIFont fontWithName:#"Helvetica-Bold" size:18];
[attributedString addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(53, 4)];
// Set font, notice the range is for the whole string
UIFont *fontItalics = [UIFont fontWithName:#"Helvetica-Oblique" size:18];
[attributedString addAttribute:NSFontAttributeName value:fontItalics range:NSMakeRange(71, 7)];
// Set label text to attributed string
[self.mytextView setAttributedText:attributedString];
`

Setting custom font in UITextView with attributed string

I have a UITextView which contains an attributed string configured through storyboard. Now i want a custom font to be applied. I am doing it through code
textView.font = [UIFont fontWithName:kCustomFont size:textView.font.pointSize];
The problem is fonts get changed losing all the attributes. How do i fix it?
(Working on iOS 5 and above)
What's in your kCustomFont? If the FontName is wrong, even a single Character, the standard SystemFont will be used.
You should change it via NSMutableAttributedString's attributes.
I.e. use something like:
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.text];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
[mutableSrting addAttributes:attrs range:NSMakeRange(0, [mutableString length])];
textView.attributedText = mutableString;
[mutableString release];

Resources