设置多少时间前
// 几天前 几分钟前..
+ (NSString *)updateTimeForTimeInterval:(NSInteger)timeInterval {
// 获取当前时时间戳 1466386762.345715 十位整数 6位小数
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// 创建歌曲时间戳(后台返回的时间 一般是13位数字)
NSTimeInterval createTime = timeInterval;
// 时间差
NSTimeInterval time = currentTime - createTime;
if (time < 60) {
return @"刚刚";
}
NSInteger minutes = time / 60;
if (minutes < 60) {
return [NSString stringWithFormat:@"%ld分钟前", minutes];
}
// 秒转小时
NSInteger hours = time / 3600;
if (hours < 24) {
return [NSString stringWithFormat:@"%ld小时前",hours];
}
// 秒转天数
NSInteger days = time / 3600 / 24;
if (days < 30) {
return [NSString stringWithFormat:@"%ld天前",days];
}
// 秒转月
NSInteger months = time / 3600 / 24 / 30;
if (months < 12) {
return [NSString stringWithFormat:@"%ld月前",months];
}
// 秒转年
NSInteger years = time / 3600 / 24 / 30 / 12;
return [NSString stringWithFormat:@"%ld年前",years];
}
设置多样文本 - 关键字、字体大小、高亮字体、字体颜色以及行间距
+ (NSAttributedString *)attStringWithString:(NSString *)string
keyWord:(NSString *)keyWord
font:(UIFont *)font
highlightedColor:(UIColor *)highlightedcolor
textColor:(UIColor *)textColor
lineSpace:(CGFloat)lineSpace {
if (string.length) {
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:string];
if (!keyWord || keyWord.length == 0) {
NSRange allRange = NSMakeRange(0, string.length);
[attStr addAttribute:NSFontAttributeName value:font range:allRange];
[attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpace;
[attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
return attStr;
}
NSRange range = [string rangeOfString:keyWord options:NSCaseInsensitiveSearch];
// 找到了关键字
if (range.location != NSNotFound) {
NSRange allRange = NSMakeRange(0, string.length);
[attStr addAttribute:NSFontAttributeName value:font range:allRange];
[attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
[attStr addAttribute:NSForegroundColorAttributeName value:highlightedcolor range:range];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpace;
[attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
} else {
NSRange allRange = NSMakeRange(0, string.length);
[attStr addAttribute:NSFontAttributeName value:font range:allRange];
[attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpace;
[attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
return attStr;
}
return attStr.copy;
}
return [[NSAttributedString alloc] init];
}
判断是否为空字符串
/**
* 判断字符串是否为空
*/
+ (BOOL)isBlankString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([string isKindOfClass:[NSDictionary class]]) {
return YES;
}
if ([string isKindOfClass:[NSString class]] == NO) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
return YES;
}
if ([string isEqualToString:@""]) {
return YES;
}
return NO;
}
把颜色转换成图片
+(UIImage *)imageWithColor:(UIColor *)color
{
//1.设定图片大小
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//2.开始图片上下文
UIGraphicsBeginImageContext(rect.size);
//3.拿到当前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//4.设置上下文颜色
CGContextSetFillColorWithColor(context,color.CGColor);
//5.填充上下文
CGContextFillRect(context, rect);
//6.从当前图片上下文获取图片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//7.图片上下文
UIGraphicsEndImageContext();
return image;
}
以上几点是我在别人的项目中新学到的知识点,以后会分享更多!