0
NSString * strTimeBefore = [timeBefore componentsJoinedByString:@" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:@" "];

I want the resulting string to be an NSAttributedString where the time in strTimeAfter is in bold

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
user4951
  • 32,206
  • 53
  • 172
  • 282

2 Answers2

1

You probably want something like:

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:@"%@ %@", strTimeBefore, strTimeAfter;
// start at the end of strTimeBefore and go the length of strTimeAfter
NSRange boldedRange = NSMakeRange([strTimeBefore length] + 1, [strTimeAfter length]);

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

[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];

And my answer is cribbed from Jacob's answer to this very closely related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • After that can I trim the resulting string? – user4951 Mar 23 '14 at 09:17
  • You mean NSFontAttributeName? What is kCTFontAttributeName – user4951 Mar 23 '14 at 09:21
  • Sometimes either strTimeBefore or strTimeAfter may be an empty string. I want an elegant way to trim them afterward. – user4951 Mar 23 '14 at 09:24
  • Yes, because it's a *mutable* attributed string, you can trim it via [`deleteCharactersInRange:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableAttributedString/deleteCharactersInRange:). As for `NSFontAttributeName` vs. `kCTFontAttributeName`, try using the first before the second... the correct attribute name to use isn't totally clear. – Michael Dautermann Mar 23 '14 at 09:26
0

Take two attribute string in that store your first string into one attribute string without changing its attributes, In second attribute string store your second string with changing its attibutes and then append both attribute string into one NSMutableAttributeString Try like this below:-

NSString * strTimeBefore = [timeBefore componentsJoinedByString:@" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:@" "];   
NSAttributedString *attrBeforeStr=[[NSAttributedString alloc]initWithString:strTimeBefore];    
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:@"Arial" traits:NSBoldFontMask weight:5 size:14];
[attributes setObject:font forKey:NSFontAttributeName];
NSAttributedString *attrAftStr=[[NSAttributedString alloc]initWithString:strTimeAfter attributes:];
NSMutableAttributedString *string=[[NSMutableAttributedString alloc] init];   
[string appendAttributedString:attrBeforeStr];
[string appendAttributedString:strTimeAfter];

Note: You can change font color as well in attribute string, if it is required.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56