[英]how to store NSMutableAttributedString to an array
I am trying to customize a string "Add as Event 1"("Event 1" is bold) and then store the string to a mutable array. 我正在尝试自定义字符串“添加为事件1”(“事件1”为粗体),然后将字符串存储到可变数组中。 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. 但是在eventsData数组中,我发现attrString1的所有约束都已存储。 But I need only the custom string in the array, because I am gonna pass the value to a label in UITableviewCell later.
但是我只需要数组中的自定义字符串,因为稍后我将把值传递给UITableviewCell中的标签。 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.
非常感谢。
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. ***有机会改进创建属性字符串和eventsData数组的代码,但我认为这是主要问题附带的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.