CNLiveMessageContentButton.m 3.14 KB
//
//  CNMessageContentButton.m
//  CNLiveSettingModule
//
//  Created by open on 2018/9/1.
//  Copyright © 2018年 cnlive. All rights reserved.
//

#import "CNLiveMessageContentButton.h"
#import "CNLiveBaseKit.h"

@interface CNLiveMessageContentButton()

@end

@implementation CNLiveMessageContentButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.content];
    }
    return self;
}

#pragma mark -文本视图
- (UILabel *)content{
    if(!_content){
        _content = [[UILabel alloc] init];
        _content.userInteractionEnabled = NO;
        _content.backgroundColor = [UIColor clearColor];
        _content.numberOfLines = 0;
        _content.lineBreakMode = NSLineBreakByWordWrapping;
        _content.font = UIFontCNMake(15);
    }
    return _content;
}

- (void)setMessageFrame:(CNLiveMessageFrame *)messageFrame {
    _messageFrame = messageFrame;
    self.frame = messageFrame.contentFrame;

    //背景气泡图
    UIImage *normal;
    CNLiveMessageModel *message = messageFrame.message;
    
    if ([message.fromMe isEqualToString:@"1"]) {
        normal = [self xw_getImageName:@"font_size_message_me" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5 topCapHeight:normal.size.height * 0.9];
    }
    else{
        normal = [self xw_getImageName:@"font_size_message_other" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5 topCapHeight:normal.size.height * 0.9];
    }
    
    [self setBackgroundImage:normal forState:UIControlStateNormal];
    
     // 我发出的消息
     if ([message.fromMe isEqualToString:@"1"]){
         // 是我的消息
         self.content.textColor = [UIColor whiteColor];
         self.content.frame = CGRectMake(ChatContentRight, ChatContentTop, self.frame.size.width-ChatContentLeft-ChatContentRight, self.frame.size.height-2*ChatContentTop);
     }
     else {
         // 不是我的消息
           self.content.frame = CGRectMake(ChatContentLeft, ChatContentTop, self.frame.size.width-ChatContentLeft-ChatContentRight, self.frame.size.height-2*ChatContentTop);

     }
    
    self.content.hidden = NO;
    self.content.text = message.content;
    self.content.font = UIFontCNMake(15);
    
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}

@end