单行及多行图文混排,聊天应用较常用,本文只提供算法。(三)

2014-11-23 22:08:39 ? 作者: ? 浏览: 6
; 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

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: