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

2014-11-23 22:08:39 ? 作者: ? 浏览: 6
HeightDefault); } UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth, self.previousHeight, width, kChatTextHeightDefault)]; l.backgroundColor = [UIColor clearColor]; l.font = kFontOfSize(fontSize); l.text = text; l.textColor = color; l.lineBreakMode = NSLineBreakByCharWrapping; // l.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:l]; if (line == [sectionStrs count] - 1) { // Last Line, recode width. viewWidth += width; } line++; } } break; case kChatRoomRemoteDynamicImage: case kChatRoomLocalDynamicImage: { NSString *imageStr = [self chatImagePath:dict]; CGFloat imageWidth = [self chatImageWidth:dict]; CGFloat imageHeight = [self chatImageHeight:dict]; CGFloat imagePaddingX = [self chatImagePaddingX:dict]; CGFloat imagePaddingY = [self chatImagePaddingY:dict]; if (viewWidth + imageWidth > kChatViewWidthMax) { // new line self.previousHeight += viewHeightMax; viewHeightMax = MAX(viewHeightMax, imageHeight); viewWidth = 0.0f; } imageStr = [imageStr substringFromIndex:kChatCutOffContentLocalDynamicImageType.length]; // LOGINFO(@"imageStr = %@", imageStr); UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(viewWidth + imagePaddingX, self.previousHeight + imagePaddingY, imageWidth, imageHeight)]; imageView.image = [UIImage sd_animatedGIFNamed:imageStr]; [self addSubview:imageView]; viewWidth += imageWidth; viewWidth += imagePaddingX; viewHeightMax = MAX(imageHeight, viewHeightMax); } break; case kChatRoomLocalStaticImage: case kChatRoomRemoteStaticImage: { NSString *imageStr = [self chatImagePath:dict]; CGFloat imageWidth = [self chatImageWidth:dict]; CGFloat imageHeight = [self chatImageHeight:dict]; CGFloat imagePaddingX = [self chatImagePaddingX:dict]; CGFloat imagePaddingY = [self chatImagePaddingY:dict]; // LOGINFO(@"imageStr = %@", imageStr); if (viewWidth + imageWidth > kChatViewWidthMax) { // new line self.previousHeight += viewHeightMax; viewHeightMax = MAX(viewHeightMax, imageHeight); viewWidth = 0.0f; } imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length]; UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(viewWidth + imagePaddingX, self.previousHeight + imagePaddingY, imageWidth, imageHeight)]; [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil]; [self addSubview:imageView]; viewWidth += imageWidth; viewWidth += imagePaddingX; viewHeightMax = MAX(imageHeight, viewHeightMax); } break; default: break; } } CGFloat totalHeight = self.previousHeight + viewHeightMax; if (totalHeight == 0.0f) { // Text Single Line; totalHeight = kChatTextHeightDefault; } self.frame = CGRectMake(self.padding, 0.0f, kChatViewWidthMax, totalHeight); return totalHeight; } - (NSArray *)splitString:(NSString *)str start:(CGFloat)loc size:(CGFloat)fontSize { NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; NSUInteger i = 1; NSUInteger location = 0; NSUInteger startPoint = 0; CGFloat startPaddingX = loc; // LOGINFO(@"str = %@ padding = %.2f", str, x)
-->

评论

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