How to BOLD certain words within an NSString paragraph

user3079872

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!

Sid Mhatre

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I bold (or format) a piece of text within a paragraph?

Tkinter: How do I Bold 'small part' of a text within a paragraph?

JavaScript - How to add bold/strong effect to certain words in a given String

How to bold only a part of a paragraph?

How to line-through all links inside a border and bold mark certain text of a paragraph

Bold word in string only if certain words not before it

Making only certain words in line of text bold

Batch file to remove paragraph if it contains certain words

Finding words within paragraph using Python

How to bold a modified paragraph in python docx

How do I make certain words inside .val() bold in my jQuery Script?

How to get words that end with certain characters within each string r

How can i dynamically change color of certain words of a paragraph in a Text Widget in flutter

How to bold certain parts of a UITextView

How to ignore tags within a paragraph?

Hovering over keywords causes glowing on certain words in a sentence/paragraph

How to extract every bold words in a html text

how to remove bold words from python docx

How to partially bold a Paragraph in Apache POI? (Word Documents)

How to convert some text in paragraph into bold xsl fo?

Python - How to make a paragraph bold and red while writing into MS Word?

How to extract words of constant length form a paragraph?

How to make words inside a paragraph clickable in flutter?

How to catch special indicated **characters** in an NSString and bold what's in between?

DataTable : How to make a certain column bold and center ?

How to Bold certain text part of a string

How to format some text as bold within a UILabel?

How to make superscripts bold within an expression

How to bold a string within a JS object?