iOS-時間差|多樣文本|是否為空字符串|顏色轉(zhuǎn)圖片

設(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é)到的知識點,以后會分享更多!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末闯割,一起剝皮案震驚了整個濱河市彻消,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌宙拉,老刑警劉巖宾尚,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異鼓黔,居然都是意外死亡央勒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門澳化,熙熙樓的掌柜王于貴愁眉苦臉地迎上來崔步,“玉大人,你說我怎么就攤上這事缎谷【簦” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵列林,是天一觀的道長瑞你。 經(jīng)常有香客問我,道長希痴,這世上最難降的妖魔是什么者甲? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮砌创,結(jié)果婚禮上虏缸,老公的妹妹穿的比我還像新娘。我一直安慰自己嫩实,他們只是感情好刽辙,可當(dāng)我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著甲献,像睡著了一般宰缤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晃洒,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天慨灭,我揣著相機與錄音,去河邊找鬼球及。 笑死缘挑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的桶略。 我是一名探鬼主播,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼际歼!你這毒婦竟也來了惶翻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤鹅心,失蹤者是張志新(化名)和其女友劉穎吕粗,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旭愧,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡颅筋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了输枯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片议泵。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖桃熄,靈堂內(nèi)的尸體忽然破棺而出先口,到底是詐尸還是另有隱情,我是刑警寧澤瞳收,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布碉京,位于F島的核電站,受9級特大地震影響螟深,放射性物質(zhì)發(fā)生泄漏谐宙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一界弧、第九天 我趴在偏房一處隱蔽的房頂上張望凡蜻。 院中可真熱鬧,春花似錦夹纫、人聲如沸咽瓷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽茅姜。三九已至,卻和暖如春月匣,著一層夾襖步出監(jiān)牢的瞬間钻洒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工锄开, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留素标,地道東北人。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓萍悴,卻偏偏與公主長得像头遭,于是被迫代替她去往敵國和親寓免。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容