#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NSString (JFMessage)
/**
* 返回聊天信息文本的寬高
*/
+ (CGSize)sizeWithString:(NSString*)str andFont:(UIFont*)font andMaxSize:(CGSize)size;
/**
* 將毫秒轉(zhuǎn)換成日期
*/
+ (NSString *)getShowDateWithTime:(NSString *)time;
@end
#import "NSString+JFString.h"
@implementation NSString (JFMessage)
+ (CGSize)sizeWithString:(NSString*)str andFont:(UIFont*)font andMaxSize:(CGSize)size {
NSDictionary*attrs =@{NSFontAttributeName: font};
return [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
+ (NSString *)getShowDateWithTime:(NSString *)time {
/**
傳入時(shí)間轉(zhuǎn)NSDate類型
*/
NSDate *timeDate = [[NSDate alloc]initWithTimeIntervalSince1970:[time longLongValue]/1000.0];
/**
初始化并定義Formatter
:returns: NSDate
*/
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
/**
* 使用Formatter格式化時(shí)間(傳入時(shí)間和當(dāng)前時(shí)間)為NSString
*/
NSString *timeStr = [dateFormatter stringFromDate:timeDate];
NSString *nowDateStr = [dateFormatter stringFromDate:[NSDate date]];
/**
* 判斷前四位是不是本年,不是本年直接返回完整時(shí)間
*/
if ([[timeStr substringWithRange:NSMakeRange(0, 4)] rangeOfString:[nowDateStr substringWithRange:NSMakeRange(0, 4)]].location == NSNotFound) {
return timeStr;
}else{
/**
* 判斷是不是本天犬金,是本天返回HH:mm,不是返回MM-dd HH:mm
*/
if ([[timeStr substringWithRange:NSMakeRange(5, 5)] rangeOfString:[nowDateStr substringWithRange:NSMakeRange(5, 5)]].location != NSNotFound) {
return [timeStr substringWithRange:NSMakeRange(11, 5)];
}else{
return [timeStr substringWithRange:NSMakeRange(5, 11)];
}
}
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者