单行及多行图文混排,聊天应用较常用,本文只提供算法。(三)
;
while (i < str.length)
{
NSRange range = NSMakeRange(location, i - startPoint);
NSString *str1 = [str substringWithRange:range];
CGSize size = [str1 sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)];
// LOGINFO(@"str1 = %@ size.width = %.2f", str1, size.width);
if (size.width + startPaddingX + kChatSplitSingleWordWidth > kChatViewWidthMax)
{
startPaddingX = 0.0f;
location += str1.length;
startPoint = i;
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
[dictionary setValue:@(size.width) forKey:kChatParseTextWidthKeyName];
[dictionary setValue:str1 forKey:kChatParseTextContentKeyName];
[dictionary setValue:@(i) forKey:kChatParseTextCutPointKeyName];
[array addObject:dictionary];
}
i++;
}
// Last Section String.
NSString *lastSectionStr = [str substringFromIndex:location];
CGSize lastStrSize = [lastSectionStr sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
[dictionary setValue:@(lastStrSize.width) forKey:kChatParseTextWidthKeyName];
[dictionary setValue:lastSectionStr forKey:kChatParseTextContentKeyName];
[dictionary setValue:@(i) forKey:kChatParseTextCutPointKeyName];
[array addObject:dictionary];
return array;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
| 评论 |
|
|