当前位置:编程学习 > C/C++ >>

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

// TTSingleLineView.h
#import <UIKit/UIKit.h>
#import "TTLineView.h"

#define kChatRoomSingleLineSendFontSize        (18.0f)

@inte易做图ce 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, kChatRoomSingleLineDefaultHeight);
    
    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 <UIKit/UIKit.h>
#import "TTLineView.h"

@inte易做图ce 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] unsignedIn
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,