0

I am trying to customize a string "Add as Event 1"("Event 1" is bold) and then store the string to a mutable array. Here is my codes:

NSString *string1 = @"Add as Event 1";
NSMutableArray *eventsData;
NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc]initWithString:string1];
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Avenir-Book"), 14.0, NULL);
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Avenir-Heavy"), 14.0, NULL);
[attrString1 addAttribute:(id)kCTFontAttributeName
               value:(__bridge id)helvetica
               range:NSMakeRange(0, [attrString1 length])];
[attrString1 addAttribute:(id)kCTFontAttributeName
               value:(__bridge id)helveticaBold
               range:NSMakeRange(7, 7)];
eventsData = [[NSMutableArray alloc] initWithObjects: attrString1, nil];

But in the eventsData array I found all constraints with the attrString1 have been stored. But I need only the custom string in the array, because I am gonna pass the value to a label in UITableviewCell later. Here is what I stored in the array:

2015-09-22 13:22:18.574 addEventUI[33834:4374868] Add as {
NSFont = "<UICTFont: 0x7ae6bce0> font-family: \"Avenir-Book\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
}Event 1{
NSFont = "<UICTFont: 0x7ae6bdd0> font-family: \"Avenir-Heavy\"; font-weight: bold; font-style: normal; font-size: 14.00pt";
}

Can anyone tell me how to do it? Thanks very much.

Dima
  • 23,484
  • 6
  • 56
  • 83
Jennifer He
  • 75
  • 1
  • 2
  • 10

1 Answers1

0

I think you're pretty close to what you want. Create the string and add it to the array as you currently have it***. Later, if you want to use either the plain string or the attributed string, you can do this...

// assume eventsData has at least one attributed string in it
NSMutableAttributedString *attributedString = eventsData[0];

// assign the plain string to a label like this
myLabel.text = attributedString.string;

// assign it with attributes to a label like this
myLabel.attributedText = attributedString;

***There's some opportunity to improve the code that creates the attributed string and the eventsData array, but I think that's incidental to the main question.

danh
  • 62,181
  • 10
  • 95
  • 136