根据段落来拆分字符串
- (void)getParagraphStart:(nullable NSUInteger *)startPtr end:(nullable NSUInteger *)parEndPtr contentsEnd:(nullable NSUInteger *)contentsEndPtr forRange:(NSRange)range;
//startPtr:段落的开始下标
//parEndPtr:该段落的结束下标
//contentsEndPtr:一共有多少个字符
//range:在这个范围内查找.len = 0.表示搜索到结尾.
示例代码:
/** * 遍历传入的Attr,按照段落进行分割,返回分割后的数组*/
- (NSArray *)paragraphsForAttributedString:(NSAttributedString *)input {
NSMutableArray *paragraphs = [NSMutableArray array];
NSUInteger length = [input length];
NSUInteger paraStart = 0, paraEnd = 0, contentsEnd = 0;
NSRange currentRange;
while (paraEnd < length) {
[input.string getParagraphStart:¶Start end:¶End
contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
if (currentRange.length > 0)
[paragraphs addObject:[input attributedSubstringFromRange:currentRange]];
else
[paragraphs addObject:[[NSAttributedString alloc] init]];
}
return paragraphs;
}
根据指定字符来拆分字符串,但该方式忽略了\r,\n或unicode字符,如有有需要请用上面的方式.
> - (NSArray*)componentsSeparatedByString:(NSString *)separator;