环信3.0功能性代码封装(二)

以下代码都是封装好的,可以直接调用

1.取出消息中的文本####

#pragma mark - 取出消息中的文本
+(NSString *)textFromMessage:(EMMessage *)message
{
    EMMessageBody *body = message.body;
    EMTextMessageBody *textBody = (EMTextMessageBody *)body;
    NSString *text = textBody.text;
    return text;
}

2.取出语音时间####

#pragma mark - 取出语音时间
+(NSString*)voiceFromMessage:(int)duration
{
    NSString *voiceDuration=[NSString stringWithFormat:@"%d''",duration];
    return voiceDuration;
}

3.取出位置中的文本####

#pragma mark - 取出位置中的文本
+(NSString *)locationFromMessage:(EMMessage *)message
{
    EMMessageBody *body = message.body;
    EMLocationMessageBody *locationBody = (EMLocationMessageBody *)body;
    NSString *address = locationBody.address;
    return address;
}

4.取出图片计算高####

#pragma mark - 取出图片计算高
+(CGFloat)imgFromMessage:(EMMessage*)message
{
    EMMessageBody *mesBody = message.body;
    EMImageMessageBody *body=((EMImageMessageBody*)mesBody);
    return EMImageHeihgt;
}
#define EMImageHeihgt  300*body.size.height/(body.size.width+body.size.height)

5.取出消息中的文本####

#pragma mark计算文本高度
+(CGSize)heightWithLable:(NSString*)txtStr
                 andFont:(CGFloat)font
                andWidth:(CGFloat)width
{
    return [txtStr boundingRectWithSize:CGSizeMake(width+30, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size;
}
//多加30是因为文字是放在button里面,边距距上下都是15

6.根据时间戳进行时间判断####

#pragma mark根据时间戳进行时间判断
+(NSString *)timeStr:(long long)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay = components.day;
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay = components.day;
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay)
    {
        //今天
        dateFmt.dateFormat = @"HH:mm";
    }
    else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay)
    {
        //昨天
        dateFmt.dateFormat = @"昨天 HH:mm";
    }
    else if(currentYear==msgYear && currentMonth==msgMonth && currentDay-msgDay>1 &¤tDay-msgDay<=7)
    {
        //当前时间的2天之前且一周之内
        NSString *week=[MessageTool getWeekDayFordate:timestamp];
        dateFmt.dateFormat=[NSString stringWithFormat:@"%@ HH:mm",week];

    }
    else if(currentYear-msgYear>=1)
    {
        //今年之前
        dateFmt.dateFormat=@"yyyy-MM-dd HH:mm";
    }
    else
    {
        //前天前
        dateFmt.dateFormat = @"MM-dd HH:mm";
    }
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}

#pragma mark根据时间戳获取周几
+(NSString *)getWeekDayFordate:(long long)data
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components =[calendar components:NSCalendarUnitWeekday fromDate:newDate];
    NSString *weekStr =[weekday objectAtIndex:components.weekday];
    return weekStr;
}

//用的时候直接调用+(NSString *)timeStr:(long long)timestamp这个方法

6.取出图片计算高####

#pragma mark 类型为图片的时候做的操作
/**
 *  图片
 *  @param imgV     赋值图片
 *  @param bgImgStr 图片底部气泡
 */
+(void)setBtnImgVWithChatImgV:(UIImageView*)imgV
                 andbgImgVStr:(NSString*)bgImgStr
{

    UIImageView *bgImgV=[[UIImageView alloc]init];
    bgImgV.image=[UIImage imageNamed:bgImgStr];
    bgImgV.frame=imgV.frame;
    //拉伸
    [bgImgV setImage:[bgImgV.image stretchableImageWithLeftCapWidth:30 topCapHeight:30]];

    CALayer *layer= bgImgV.layer;
    layer.frame    = (CGRect){{0,0},bgImgV.layer.frame.size};
    imgV.layer.mask = layer;
    [imgV setNeedsDisplay];
}

7.图片循环动画####

你可能感兴趣的:(环信3.0功能性代码封装(二))