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

2014-11-23 22:08:39 ? 作者: ? 浏览: 5
// TTSingleLineView.h
#import 
#import "TTLineView.h"

#define kChatRoomSingleLineSendFontSize        (18.0f)

@interface TTSingleLineView : TTLineView

+ (TTSingleLineView *)singleLineView:(NSArray *)views;

@end

//TTSingleLineView.m
#import "TTSingleLineView.h"

#define kChatRoomSingleLineDefaultHeight (30.0f)

@implementation TTSingleLineView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor colorWithWhite:0.4f alpha:0.4f];
    }
    return self;
}

+ (TTSingleLineView *)singleLineView:(NSArray *)views
{
    TTSingleLineView *slv = [[TTSingleLineView alloc] initWithFrame:CGRectZero];
    [slv chatRoomSingleLineView:views];
    return slv;
}

- (CGFloat)chatRoomSingleLineView:(NSArray *)array
{
    CGFloat lineWidth = 0.0f;
    for (NSDictionary *dict in array)
    {
        switch ([self contentType:dict])
        {
            case kChatRoomText:
            {
                NSString *contentText = [self chatContentText:dict];
                CGFloat fontSize = [self chatContentTextFont:dict];
                CGFloat textWidth = [self getStringWidth:contentText size:fontSize];
                ChatRoomFontColorMode colorMode = [[dict valueForKey:kChatTextColorTypeKeyName] integerValue];
                UIColor *color = [self chatTextColor:colorMode];
                
                UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(lineWidth,
                                                                       0.0f,
                                                                       textWidth,
                                                                       kChatRoomSingleLineDefaultHeight)];
                l.backgroundColor = [UIColor clearColor];
                l.font = kFontOfSize(fontSize);
                l.text = contentText;
                l.textColor = color;
                l.lineBreakMode = NSLineBreakByCharWrapping;
                [self addSubview:l];
                
                lineWidth += textWidth;
            }
                break;
            case kChatRoomRemoteDynamicImage:
            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];
                
                imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(lineWidth + imagePaddingX,
                                                     0.0f + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];
                [self addSubview:imageView];
                
                lineWidth += imageWidth;
                lineWidth += imagePaddingX;
            }
                break;
            case kChatRoomLocalDynamicImage:
            case kChatRoomLocalStaticImage:
            {
                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];
                               
                imageStr = [imageStr substringFromIndex:kChatCutOffContentRemoteStillImageType.length];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(lineWidth + imagePaddingX,
                                                     0.0f + imagePaddingY,
                                                     imageWidth,
                                                     imageHeight)];
                imageView.image = kImageNamed(imageStr);
                [self addSubview:imageView];
                
                lineWidth += imageWidth;
                lineWidth += imagePaddingX;
            }
                break;
                
            default:
                break;
        }
    }

    self.frame = CGRectMake(0.0f, 0.0f, lineWidth, kChatRoomS
            
-->

评论

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