Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9212799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:36:28+00:00 2026-06-18T01:36:28+00:00

Say I have NSString * hello = @hello world; Now I want an atributed

  • 0

Say I have NSString * hello = @”hello world”;

Now I want an atributed string where the hell in hello world is bolded.

There is a function on the web that do this:

- (NSMutableAttributedString*) word:(NSString*)substringToHighlight{

    NSMutableAttributedString * mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:self];
    NSUInteger count = 0, length = [mutableAttributedString length];
    NSRange range = NSMakeRange(0, length);

    count = 0,
    length = [mutableAttributedString length];
    range = NSMakeRange(0, length);
    while(range.location != NSNotFound)
    {
        range = [[mutableAttributedString string] rangeOfString:substringToHighlight options:0 range:range];
        if(range.location != NSNotFound) {

            //[mutableAttributedString setTextColor:[UIColor blueColor] range:NSMakeRange(range.location, [word length])];
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            count++;
        }
    }
    return mutableAttributedString;
}

However, the function doesn’t work because mutableAttributedString doestn’t support setTextColor

I also tried

NSDictionary * dict = @{kCTFontAttributeName:boldFontName};
        [mutableAttributedString setAttributes:{kCTFontAttributeName:boldFontName} range:NSMakeRange(range.location, substringToHighlight.length)];

but got a message kCTFontAttributeName is not defined.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:36:29+00:00Added an answer on June 18, 2026 at 1:36 am

    You can use rangeOfString:options:range: or NSScanner (there are other possibilities like regexps but anyway).

    Finds and returns the range of the first occurrence of a given string, within the given range of the receiver, subject to given options.

    • (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange

    This is another solution :

    Then you need to convert into NSMutableAttributedString like this way.

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello world"];
    NSRange selectedRange = NSMakeRange(0, 4); // 4 characters, starting at index 0
    
    [string beginEditing];
    
    [string addAttribute:NSFontAttributeName
               value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0]
               range:selectedRange];
    
    [string endEditing];
    

    I think this is the best solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an NSString (or NSMutableString) containing: I said Hello, world!. He said
Say I have a string: NSString *state = @"California, CA"; Can someone please tell
Say I have a number, double, or NSNumber, 3.333333 I want that to turn
Say I have a NSString NSString *myIpAddress = @192.168.1.1 I want to convert this
Let's say that I have declared an NSString like this NSString *myString = [[NSString
I have an NSString, let's say Hello... What is the quickest way to fill
Say I have my class @interface Person : NSObject { NSString *name; } I
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Say I have a SSI script that uses exec, or a PHP script that
Say I have: @property (nonatomic,retain) NSString *foo; in some class. And I call: myclass.foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.