How can you take an existing NSAttributedString and divide it based on a predefined separator while maintaining formatting? It does not seem that componentsSeparatedByString will operate on an NSAttributedString.
如何使用现有的NSAttributedString并基于预定义的分隔符进行划分,同时维护格式?似乎组件分离的bystring并不会对NSAttributedString进行操作。
My current workaround produces the splits at the correct points, but only outputs an NSString. Thus losing formatting.
我目前的工作是在正确的点上产生分割,但只输出一个NSString。因此失去格式化。
NSData *rtfFileData = [NSData dataWithContentsOfFile:path];
NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
NSString *rtfFileString = [rtfFileAttributedString string];
NSString *importSeparator = @"###";
// Wish I could do this
// NSArray *separatedArray = [rtfFileAttributedString componentsSeparatedByString:importSeparatorPref];
NSArray *separatedArray = [rtfFileString componentsSeparatedByString:importSeparatorPref];
NSLog( @"Separated array: %@", separatedArray );
1 个解决方案
#1
8
You can make use of your split non-attributed string to split up the attributed string. One option would be:
您可以使用拆分的非属性字符串来分割属性字符串。一个选择是:
NSData *rtfFileData = [NSData dataWithContentsOfFile:path];
NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
NSString *rtfFileString = [rtfFileAttributedString string];
NSString *importSeparator = @"###";
NSArray *separatedArray = [rtfFileString componentsSeparatedByString:importSeparatorPref];
NSMutableArray *separatedAttributedArray = [NSMutableArray arrayWithCapacity:separatedArray.count];
NSInteger start = 0;
for (NSString *sub in separatedArray) {
NSRange range = NSMakeRange(start, sub.length);
NSAttributedString *str = [rtfFileAttributedString attributedSubstringFromRange:range];
[separatedAttributedArray addObject:str];
start += range.length + importSeparator.length;
}
NSLog(@"Separated attributed array: ", separatedAttributedArray);
#1
8
You can make use of your split non-attributed string to split up the attributed string. One option would be:
您可以使用拆分的非属性字符串来分割属性字符串。一个选择是:
NSData *rtfFileData = [NSData dataWithContentsOfFile:path];
NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
NSString *rtfFileString = [rtfFileAttributedString string];
NSString *importSeparator = @"###";
NSArray *separatedArray = [rtfFileString componentsSeparatedByString:importSeparatorPref];
NSMutableArray *separatedAttributedArray = [NSMutableArray arrayWithCapacity:separatedArray.count];
NSInteger start = 0;
for (NSString *sub in separatedArray) {
NSRange range = NSMakeRange(start, sub.length);
NSAttributedString *str = [rtfFileAttributedString attributedSubstringFromRange:range];
[separatedAttributedArray addObject:str];
start += range.length + importSeparator.length;
}
NSLog(@"Separated attributed array: ", separatedAttributedArray);