CNLiveMessageContentButton.m
3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// 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