iOS环信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];
}

效果是这样的:


iOS环信3.0项目中用到的一些封装的类方法_第1张图片
气泡图片效果

7.图片循环动画

#pragma mark 图片动画
/**
 *  图片动画
 *
 *  @param num               图片张数
 *  @param imgStr            图片名字
 *  @param animationDuration 图片动画时长
 *  @param repeatTime        图片动画重复次数
 *
 *  @return 图片动画
 */
+(void)imgVVoiceWithNum:(int)num
              andImgStr:(NSString*)imgStr
                andImgV:(UIImageView*)imgV
   andAnimationDuration:(float)animationDuration
          andRepeatTime:(int)repeatTime
{
    //创建一个数组,数组中按顺序添加要播放的图片(图片为静态的图片)
    NSMutableArray *imgArray = [NSMutableArray array];
    for (int i=1; i<=num; i++)
    {
        NSString *str=[NSString stringWithFormat:@"%@%d@2x",imgStr,i];
        UIImage *image =Image(str);
        [imgArray addObject:image];
    }
    //把存有UIImage的数组赋给动画图片数组
    imgV.animationImages = imgArray;
    //设置执行一次完整动画的时长
    imgV.animationDuration = animationDuration;
    //动画重复次数 (0为重复播放)
    imgV.animationRepeatCount =repeatTime ;
    //开始播放动画
    [imgV startAnimating];
}

好啦,以上封装很实用,明天再整环信啦!

用到的环信方法

环信3.0发送文字,语音,位置,图片封装代码

你可能感兴趣的:(iOS环信3.0项目中用到的一些封装的类方法)