跟上篇 腾讯云 IM iOS 自定义消息 类似
子类化QYCustomContentView
h文件
#import <UIKit/UIKit.h>
#import <QYSDK/QYCustomSDK.h>
NS_ASSUME_NONNULL_BEGIN
@interface QYCustomOrderContentView : QYCustomContentView
@property(nonatomic, strong) UILabel *m_titleLab;
@property(nonatomic, strong) UILabel *m_subTitleLab;
@property(nonatomic, strong) UILabel *m_priceLab;
/// 头像
@property(nonatomic, strong) UIImageView *m_imgV;
#pragma mark **- 订单**
/// 付款提醒前的图标
@property(nonatomic, strong) UIImageView *m_tipImgV;
/// 付款提醒
@property(nonatomic, strong) UILabel *m_tipLab;
@property(nonatomic, strong) UIView *m_content;
@end
NS_ASSUME_NONNULL_END
m文件
#import "QYCustomOrderContentView.h"
#import "QYCustomOrderMessage.h"
#import "TUIDefine.h"
#import "QYCustomOrderModel.h"
@implementation QYCustomOrderContentView
- (instancetype)initCustomContentView {
return [QYCustomOrderContentView new];
}
- (void)refreshData:(QYCustomModel *)model {
[super refreshData:model];
if ([model isKindOfClass:[QYCustomOrderModel class]]) {
QYCustomOrderModel *obj = (QYCustomOrderModel *)model;
QYCustomMessage *cs = obj.message;
if ([cs isKindOfClass:[QYCustomOrderMessage class]]) {
QYCustomOrderMessage *data = (QYCustomOrderMessage *)cs;
self.m_titleLab.text = data.title;
self.m_subTitleLab.text = data.subTitle;
self.m_priceLab.text = data.price;
[self.m_imgV nwSetImage:data.img];
}
}
}
-(UILabel *)m_titleLab {
if (_m_titleLab) {
return _m_titleLab;
}
_m_titleLab = ({
UILabel *lab = [UILabel new];
lab.text = @"培训商品标题";
lab.font = [UIFont fontWithName:@"PingFangSC-Medium" size: 16];
// #000000 80%
lab.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
lab.opaque = YES;
lab.backgroundColor = [UIColor whiteColor];
lab;
});
return _m_titleLab;
}
-(UILabel *)m_subTitleLab {
if (_m_subTitleLab) {
return _m_subTitleLab;
}
_m_subTitleLab = ({
UILabel *lab = [UILabel new];
lab.text = @"副标题";
lab.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 12];
// #000000 64%
lab.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.64];
lab.opaque = YES;
lab.backgroundColor = [UIColor whiteColor];
lab;
});
return _m_subTitleLab;
}
-(UILabel *)m_priceLab {
if(_m_priceLab){
return _m_priceLab;
}
_m_priceLab = ({
UILabel *lab = [UILabel new];
lab.text = @"¥2131";
lab.font = [UIFont fontWithName:@"DIN Condensed Bold" size: 24];
// #000000 80%
lab.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
lab.opaque = YES;
lab.backgroundColor = [UIColor whiteColor];
lab;
});
return _m_priceLab;
}
-(UIImageView *)m_imgV {
if(_m_imgV){
return _m_imgV;
}
_m_imgV = [UIImageView new];
return _m_imgV;
}
-(UIImageView *)m_tipImgV {
if(_m_tipImgV){
return _m_tipImgV;
}
_m_tipImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_green_#"]];
return _m_tipImgV;
}
- (UILabel *)m_tipLab {
if(_m_tipLab){
return _m_tipLab;
}
_m_tipLab = ({
UILabel *lab = [UILabel new];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 2;
shadow.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.08];
shadow.shadowOffset = CGSizeMake(0,1);
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"付款提醒" attributes:@{
NSFontAttributeName: FAlibabaPuHuiTi(16),
NSForegroundColorAttributeName : [UIColor colorWithRed:64/255.0 green:169/255.0 blue:235/255.0 alpha:1.0],
NSShadowAttributeName: shadow}];
lab.attributedText = string;
lab.opaque = YES;
lab.backgroundColor = [UIColor whiteColor];
lab;
});
return _m_tipLab;
}
-(UIView *)m_content {
if (_m_content) {
return _m_content;
}
UIView *view = [UIView new];
view.frame = CGRectMake(56,130,258,104+28);
view.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
view.layer.cornerRadius = 16;
view.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.08].CGColor;
view.layer.shadowOffset = CGSizeMake(0,0);
view.layer.shadowOpacity = 1;
view.layer.shadowRadius = 8;
_m_content = view;
return _m_content;
}
- (void)layoutSubviews{
[super layoutSubviews];
// 这里写法有点诡异,自从项目引入了一些三方后,UIView的生命周期都变了,得判断下有自己在没有父控件一,没有就上去
if (!self.m_content.superview) {
[self addSubview:self.m_content];
}
if ( self.m_content.layer.shadowPath == nil) {
self.m_content.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.m_content.bounds cornerRadius:16].CGPath;
}
self.m_content.frame = CGRectMake(CGRectGetWidth(self.frame)-250-8, 8, 250, 104+28);
if (!self.m_tipImgV.superview) {
[self.m_content addSubview:self.m_tipImgV];
}
self.m_tipImgV.mm_top(12).mm_left(11);
if (!self.m_tipLab.superview) {
[self.m_content addSubview:self.m_tipLab];
}
[self.m_tipLab sizeToFit];
self.m_tipLab.mm_left(CGRectGetMaxX(self.m_tipImgV.frame)+8);
self.m_tipLab.mm_centerY = self.m_tipImgV.centerY;
if (!self.m_imgV.superview) {
[self.m_content addSubview:self.m_imgV];
} self.m_imgV.mm_top(CGRectGetMaxY(self.m_tipImgV.frame)+5).mm_left(12).mm_width(80).mm_height(80);
CGFloat labLeft = CGRectGetMaxX(self.m_imgV.frame)+8;
if (!self.m_titleLab.superview) {
[self.m_content addSubview:self.m_titleLab];
}
self.m_titleLab.mm_sizeToFit().mm_top(CGRectGetMinY(self.m_imgV.frame)).mm_left(labLeft).mm_flexToRight(10);
if ( !self.m_subTitleLab.superview) {
[self.m_content addSubview:self.m_subTitleLab];
}
self.m_subTitleLab.mm_sizeToFit().mm_top(CGRectGetMaxY(self.m_titleLab.frame)+1).mm_left(labLeft).mm_flexToRight(10);
if ( !self.m_priceLab.superview) {
[self.m_content addSubview:self.m_priceLab];
}
self.m_priceLab.mm_sizeToFit().mm_left(labLeft).mm_bottom(8).mm_flexToRight(10);
}
子类化QYCustomMessage
// h 文件
#import <UIKit/UIKit.h>
#import <QYSDK/QYCustomSDK.h>
NS_ASSUME_NONNULL_BEGIN
@interface QYCustomOrderMessage : QYCustomMessage
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subTitle;
@property(nonatomic, copy) NSString *price;
@property(nonatomic, copy) NSString *img;
@property(nonatomic, copy) NSString *businessID;
@end
NS_ASSUME_NONNULL_END
// m文件
#import "QYCustomOrderMessage.h"
#import <YYKit/NSObject+YYModel.h>
@implementation QYCustomOrderMessage
- (NSString *)thumbText {
return self.title;
}
- (QYCustomMessageSourceType)messageSourceType {
return QYCustomMessageSourceTypeSend;
}
- (NSDictionary *)encodeMessage {
return [self modelToJSONObject];
}
- (void)decodeMessage:(NSDictionary *)content {
[self modelSetWithDictionary:content];
}
@end
子类化QYCustomModel
// h文件
#import <UIKit/UIKit.h>
#import <QYSDK/QYCustomSDK.h>
NS_ASSUME_NONNULL_BEGIN
@interface QYCustomOrderModel : QYCustomModel
@end
NS_ASSUME_NONNULL_END
// m文件
#import "QYCustomOrderModel.h"
@implementation QYCustomOrderModel
-(NSString *)cellReuseIdentifier {
return @"QYCustomOrderCellID";
}
- (CGSize)contentSizeForBubbleMaxWidth:(CGFloat)bubbleMaxWidth {
CGFloat w = 250;
CGFloat h = 104;
return CGSizeMake(w, h+28+16);
}
-(BOOL)needShowBubble {
return NO;
}
@end
注册自定义cell
用QYSessionViewController注册
QYSessionViewController * sessionViewController = [[QYSDK sharedSDK] sessionViewController];
// 其他设置 ...
// 注册自定义cell 订单
[sessionViewController registerCustomMessageClass:[QYCustomOrderMessage class]
modelClass: [QYCustomOrderModel class]
contentViewClass:[QYCustomOrderContentView class]];
发送自定义消息
- (void)sendOrder {
QYCustomOrderMessage *msg = [QYCustomOrderMessage new];
//todo 组装msg数据
// 自定义消息标识,如custom_message_product
msg.businessID = @"custom_message_order";
//用上面的vc发消息
[self.sessionVC addCustomMessage:msg needSaveData:YES needReloadView:YES completion:^(NSError *error) {
if(error) {
FlexShowToast(error.localizedDescription, 2);
}
}];
}