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

2014-11-23 22:08:39 ? 作者: ? 浏览: 8
ingleLineDefaultHeight); return kChatRoomSingleLineDefaultHeight; } - (CGFloat)getStringWidth:(NSString *)text size:(CGFloat)fontSize { CGSize size = [text sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatRoomSingleLineDefaultHeight)]; return size.width; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end // TTMultiLineView.h #import #import "TTLineView.h" @interface TTMultiLineView : TTLineView @property (nonatomic, assign) CGFloat previousHeight; // View's total height. @property (nonatomic, assign) CGFloat height; @property (nonatomic, assign) CGFloat padding; + (TTMultiLineView *)multiLineView:(NSArray *)views; @end // TTMultiLineView.m #import "TTMultiLineView.h" #define kChatTextHeightDefault (24.0f) #define kChatViewWidthMax (kScreenWidth - self.padding * 2) #define kChatSplitSingleWordWidth (15.0f) #define kChatContentPadding (0.0f) // Parse. #define kChatParseTextWidthKeyName @"chat.parse.text.width" #define kChatParseTextContentKeyName @"chat.parse.text.content" #define kChatParseTextCutPointKeyName @"chat.parse.text.cutpoint" @implementation TTMultiLineView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } + (TTMultiLineView *)multiLineView:(NSArray *)views { TTMultiLineView *mlv = [[TTMultiLineView alloc] initWithFrame:CGRectZero]; [mlv chatMultiLineView:views]; return mlv; } - (CGFloat)chatMultiLineView:(NSArray *)array { CGFloat viewWidth = 0.0f; CGFloat viewHeightMax = 0.0f; if (self.padding == 0.0f) { self.padding = kChatContentPadding; } for (NSDictionary *dict in array) { switch ([self contentType:dict]) { case kChatRoomText: { NSString *contentText = [self chatContentText:dict]; CGFloat fontSize = [self chatContentTextFont:dict]; if (contentText != nil && contentText.length > 0) { CGSize firstCharSize = [[contentText substringToIndex:1] sizeWithFont:kFontOfSize(fontSize) constrainedToSize:CGSizeMake(MAXFLOAT, kChatTextHeightDefault)]; if (viewWidth + firstCharSize.width > kChatViewWidthMax) { // insert new line if last string reach end. self.previousHeight += kChatTextHeightDefault; viewWidth = 0.0f; } } NSArray *sectionStrs = [self splitString:contentText start:viewWidth size:fontSize]; // Multiline. NSUInteger line = 0; for (NSDictionary *d in sectionStrs) { NSUInteger width = [[d valueForKey:kChatParseTextWidthKeyName] unsignedIntegerValue]; NSString *text = [d valueForKey:kChatParseTextContentKeyName]; ChatRoomFontColorMode colorMode = [[dict valueForKey:kChatTextColorTypeKeyName] integerValue]; UIColor *color = [self chatTextColor:colorMode]; if (line != 0) { viewWidth = 0.0f; if (line == 1) { // First Line self.previousHeight += MAX(kChatTextHeightDefault, viewHeightMax); viewHeightMax = kChatTextHeightDefault; } else { // > 1 line. self.previousHeight += kChatTextHeightDefault; } } else { viewHeightMax = MAX(viewHeightMax, kChatText
-->

评论

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