我想问一下如何将x个属性字符串组合起来并制作一个字符串。
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:health.storeArry];
NSLog(@"%lu",(unsigned long)array.count);
for (NSDate * str in array) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
NSString * dateTimeString = [dateFormatter stringFromDate:str];
NSString* str=dateTimeString;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
[attrString addAttribute: NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:15] range: NSMakeRange(str.length-2,2)];
NSLog(@"%@",attrString);
[attrString appendAttributedString:attrString];
}
这是我的代码。我找到了两个或三个attibutedstring cancatenated但我有一个数组,其中x号nsdate
个对象。我想在一个字符串中连接所有这些。我怎样才能做到这一点?
感谢
答案 0 :(得分:0)
做这样的事,
NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSMutableString *str = [[NSMutableString alloc]init];
for (int i = 0; i < arr.count; i++) {
str = [str stringByAppendingString:[arr objectAtIndex:i]];
}
NSLog(@"final str is %@",str);
希望这会有所帮助:)
更新:
您可以转换为NSString
喜欢,
NSString *str1 = [NSString stringWithFormat:@"%@", nsattributedstring];
然后你可以追加它。所以从数组中获取属性字符串将其转换为nsstring并附加它。
答案 1 :(得分:0)
您的问题是每次新NSMutableAttributedString
时都在创建。你需要把它放在for循环之外。
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:health.storeArry];
NSLog(@"%lu",(unsigned long)array.count);
NSMutableAttributedString *finalAttributedString = [NSMutableAttributedString alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
for (NSDate *str in array) {
NSString *dateTimeString = [dateFormatter stringFromDate:str];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:dateTimeString];
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:15] range:NSMakeRange(str.length-2,2)];
NSLog(@"%@",attrString);
[finalAttributedString appendAttributedString:attrString];
}
//Here, you can read the finalAttributedString
不相关提示:您应该在for循环之外分配/初始化NSDateFormatter
。它总是相同的,加上NSDateFormatter
的alloc / init正在消耗。