How to BOLD certain words within an NSString paragraph - ios

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;

Related

Find last line of multiline label and change colour [duplicate]

This question already has answers here:
UILabel Text with Multiple Font Colors
(8 answers)
Closed 4 years ago.
I m working on multi line text with different colour.The following code to change the first line colour But want to change the last line colour.
NSRange rangeOfNewLine = [label.text rangeOfString:#"\n"];
NSRange newRange = NSMakeRange(0, rangeOfNewLine.location);
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:newRange];
NSArray* lines = [label.text componentsSeparatedByString:#"\n"];
NSString* lastLine = lines.lastObject;
NSRange rangeOfLastLine = [label.text rangeOfString:lastLine];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:rangeOfLastLine];
//NSString *myString = #"I have to replace text 'Dr Andrew Murphy, John Smith' ";
NSString *myString = #"Not a member?signin";
//Create mutable string from original one
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:myString];
//Fing range of the string you want to change colour
attributeText _label.attributedText = attString;
//If you need to change colour in more that one place just repeat it
NSRange range = [myString rangeOfString:#"signin"];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:(63/255.0) green:(163/255.0) blue:(158/255.0) alpha:1.0] range:range];
//Add it to the label - notice its not text property but it's
attributeText _label.attributedText = attString;

add bold text between text in UILabel [duplicate]

This question already has answers here:
How do you use NSAttributedString?
(15 answers)
Closed 7 years ago.
I to display long text with too much modification..
Large text contains special characters. Whenever special character occur below line should be bold and on occur of \n new label should generated.
Try This : you can add any font style in value parameter.Using this you can add style to substring to make it different from normal string.
NSString *strFirst = #"Anylengthtext";
NSString *strSecond = #"Anylengthtext";
NSString *strThird = #"Anylengthtext";
NSString *strComplete = [NSString stringWithFormat:#"%# %# %#",strFirst,strSecond,strThird];
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:[strComplete rangeOfString:strFirst]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIFont fontWithName:#"Roboto-Regular" size:12]
range:[strComplete rangeOfString:strSecond]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:[strComplete rangeOfString:strThird]];
self.lblName.attributedText = attributedString;

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

Two text colors in one UITableViewCell text label

I have a text label inside a UITableViewCell consisting of two words.
How can I change the color of the words; making the first word green, and the second word red?
NSString *twoWords = #"Green Red";
NSArray *components = [twoWords componentsSeparatedByString:#" "];
NSRange greenRange = [twoWords rangeOfString:[components objectAtIndex:0]];
NSRange redRange = [twoWords rangeOfString:[components objectAtIndex:1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:twoWords];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor greenColor]
range:greenRange];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor redColor]
range:greenRange];
[attrString endEditing];
Then you can use attrString directly on a UILabel (> iOS 6, check Apple Documentation).
The simplest way to do this would be with an nsattributedstring in iOS 6.0 or later. You would allocate one of those and in the titleLabel (or any other object that holds text) of the UITableViewCell. If you're using the titleLabel you would do this:
[cell.titleLabel setAttributedText:yourAttributedString];
To setup the colors with an NSAttributedString, do this:
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:stringToManipulate];
[attributedString beginEditing];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, widthOfFisrtWord)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(widthOfFisrtWord, widthOfSecondWord)];
[attributedString endEditing];
Note that the ranges provided above using NSMakeRange won't be the ranges you need. You'll have to change the range to fit your own needs depending if the two words have a space in between them or other characters.
Apple Documentation:
NSAttributedString
NSAttributedString UIKit Additions Reference
NSMutableAttributedString
This question addresses getting part of a string, which you would need to do. Instead of modifying the text with BOLD though, you can use this question to get an idea of how to change the color.
By using NSAttributedString string you can set two different colors.NSAttributedString
once check this one.

Resources