1

How can I make Bold (strong) the first 3 symbols of a NSString? For example I have string @"Hello", but I need: Hello

I have this string:

NSString *testString = @"Hello";
alexburtnik
  • 7,661
  • 4
  • 32
  • 70

2 Answers2

1

You will be use this method, in which add your original string and part of string in which you want change its font.

This is global solution for format a current string.

Syntax for Calling the method

[self getAttributedStringWithFullString:@"Hello" forRangeOfString:@"Hel" withRangeOfStringFont:[UIFont boldSystemFontOfSize:12.0f]];





-(NSAttributedString*)getAttributedStringWithFullString:(NSString*)fullString
                                       forRangeOfString:(NSString*)rangeOfString
                                  withRangeOfStringFont:(UIFont*)rangeOfStringFont
{
    NSMutableAttributedString* att_string = [[NSMutableAttributedString alloc]initWithString:fullString];
    NSRange atRange = [att_string.string rangeOfString:rangeOfString];

    if (atRange.location != NSNotFound)
    {
        [att_string addAttribute:NSFontAttributeName value:rangeOfStringFont range:atRange];
    }

    return att_string;
}
Rishi
  • 167
  • 1
  • 7
0
NSString *testString = @"Hello";

//Checking string length
if (testString.length >= 3 )
{

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:testString];

    //Bold font range
    NSRange boldFontRange = NSMakeRange(0, 3);
    [attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13.0] range:boldFontRange];

    //Setting Normal Font
    NSRange normalFontRange = NSMakeRange(3, testString.length - 3);
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:normalFontRange];


    //Setting attributed Text to Label
    [self.lblTitle setAttributedText:attrString];
}
MD.
  • 1,157
  • 11
  • 18