0

I am unable to set custom font in NSMutableAttributedString

But when setting font to System it's working fine. Here is my code:

 NSString *str=@"my attributed text";
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:str];
    UIFont *customFont =[UIFont fontWithName:@"Play-Regular" size:13.0];
    [attString addAttributes:@{
                               NSForegroundColorAttributeName: [UIColor colorWithRed:111.0/255.0 green:75.0/255.0 blue:26.0/255.0 alpha:1.0],
                               NSFontAttributeName: customFont,
                               } range:NSMakeRange(0,str.length)];

I am unable to set custom font in UISearchbar.

[[UITextField appearanceWhenContainedIn: [UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Play-Regular" size:13.0]}];

Although I have successfully added Play font.

But it working fine when setting

[[UITextField appearanceWhenContainedIn: [UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:13.0]}];
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Tapash Mollick
  • 135
  • 1
  • 11
  • 1
    Can you print stack of customFont. May be its not picking your correct font. – Dharmbir Singh Aug 10 '15 at 06:07
  • Check your font name using NSArray *allfamilies = [UIFont familyNames]; for (NSString *familyName in allfamilies) NSLog(@"fonts %@ \nin family %@",[UIFont fontNamesForFamilyName:familyName],familyName); – Raman soni Aug 10 '15 at 07:14

1 Answers1

0

Try this

    NSMutableAttributedString *attributedstring = [[NSMutableAttributedString alloc] initWithString:@"My text"];
    [attributedstring addAttribute:NSForegroundColorAttributeName value:[UIColor myColor] range:NSMakeRange(0, self.selectedbanner.bannerTitle.length)];
    [attributedstring addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"myFont" size:fontsize] range:NSMakeRange(0, self.selectedbanner.bannerTitle.length)];
    myTextField.attributedText = attributedstring;

Cross check your font name and make sure you've added that font to your project bundle

Ajith Kumar
  • 1,202
  • 1
  • 10
  • 22