設(shè)置多少時間前
// 幾天前 幾分鐘前..
+ (NSString *)updateTimeForTimeInterval:(NSInteger)timeInterval {
// 獲取當(dāng)前時時間戳 1466386762.345715 十位整數(shù) 6位小數(shù)
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// 創(chuàng)建歌曲時間戳(后臺返回的時間 一般是13位數(shù)字)
NSTimeInterval createTime = timeInterval;
// 時間差
NSTimeInterval time = currentTime - createTime;
if (time < 60) {
return @"剛剛";
}
NSInteger minutes = time / 60;
if (minutes < 60) {
return [NSString stringWithFormat:@"%ld分鐘前", minutes];
}
// 秒轉(zhuǎn)小時
NSInteger hours = time / 3600;
if (hours < 24) {
return [NSString stringWithFormat:@"%ld小時前",hours];
}
// 秒轉(zhuǎn)天數(shù)
NSInteger days = time / 3600 / 24;
if (days < 30) {
return [NSString stringWithFormat:@"%ld天前",days];
}
// 秒轉(zhuǎn)月
NSInteger months = time / 3600 / 24 / 30;
if (months < 12) {
return [NSString stringWithFormat:@"%ld月前",months];
}
// 秒轉(zhuǎn)年
NSInteger years = time / 3600 / 24 / 30 / 12;
return [NSString stringWithFormat:@"%ld年前",years];
}
設(shè)置多樣文本 - 關(guān)鍵字、字體大小、高亮字體罗标、字體顏色以及行間距
+ (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];
// 找到了關(guān)鍵字
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;
}
把顏色轉(zhuǎn)換成圖片
+(UIImage *)imageWithColor:(UIColor *)color
{
//1.設(shè)定圖片大小
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//2.開始圖片上下文
UIGraphicsBeginImageContext(rect.size);
//3.拿到當(dāng)前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//4.設(shè)置上下文顏色
CGContextSetFillColorWithColor(context,color.CGColor);
//5.填充上下文
CGContextFillRect(context, rect);
//6.從當(dāng)前圖片上下文獲取圖片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//7.圖片上下文
UIGraphicsEndImageContext();
return image;
}
以上幾點是我在別人的項目中新學(xué)到的知識點,以后會分享更多!