如何将 NSMutableAttributedString 存储到数组
how to store NSMutableAttributedString to an array
我正在尝试自定义一个字符串 "Add as Event 1"("Event 1" 是粗体),然后将该字符串存储到一个可变数组中。这是我的代码:
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];
但是我发现在 eventsData 数组中,attrString1 的所有约束都已存储。但我只需要数组中的自定义字符串,因为稍后我会将值传递给 UITableviewCell 中的标签。这是我存储在数组中的内容:
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";
}
谁能告诉我怎么做?非常感谢。
我认为你非常接近你想要的。创建字符串并将其添加到您当前拥有的数组中***。以后,如果你想使用纯字符串或属性字符串,你可以这样做...
// 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;
***有机会改进创建属性字符串和 eventsData 数组的代码,但我认为这对主要问题来说是偶然的。
我正在尝试自定义一个字符串 "Add as Event 1"("Event 1" 是粗体),然后将该字符串存储到一个可变数组中。这是我的代码:
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];
但是我发现在 eventsData 数组中,attrString1 的所有约束都已存储。但我只需要数组中的自定义字符串,因为稍后我会将值传递给 UITableviewCell 中的标签。这是我存储在数组中的内容:
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";
}
谁能告诉我怎么做?非常感谢。
我认为你非常接近你想要的。创建字符串并将其添加到您当前拥有的数组中***。以后,如果你想使用纯字符串或属性字符串,你可以这样做...
// 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;
***有机会改进创建属性字符串和 eventsData 数组的代码,但我认为这对主要问题来说是偶然的。