iOS 隨機(jī)顏色和十六進(jìn)制顏色轉(zhuǎn)換

一、隨機(jī)顏色

- (UIColor *)arndomColor

{

CGFloat red = arc4random_uniform(256)/ 255.0;

CGFloat green = arc4random_uniform(256)/ 255.0;

CGFloat blue = arc4random_uniform(256)/ 255.0;

UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

return color;

}

隨機(jī)顏色的宏:

#define ? ?random(r, g, b, a) ? ?[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define ? ?randomColor ? ? ? ?random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

注:

arc4random_uniform( )這個(gè)函數(shù)不像arc4random( )產(chǎn)生的數(shù)那么大,arc4random_uniform(n)可以產(chǎn)生1-(n-1)之間的數(shù)

二、UIColor十六進(jìn)制顏色轉(zhuǎn)換的分類:

UIColor+CustomColor.h

#import@interface UIColor (CustomColor)

/** 從十六進(jìn)制字符串獲取顏色 */

+ (UIColor *) colorWithHexString: (NSString *)color alpha:(CGFloat)alpha;

@end

UIColor+CustomColor.m

#import "UIColor+CustomColor.h"

@implementation UIColor (CustomColor)

+ (UIColor *) colorWithHexString: (NSString *)color alpha:(CGFloat)alpha

{

// 刪除字符串中的空格

NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

// String should be 6 or 8 characters

if ([cString length] < 6) {

return [UIColor clearColor];

}

// strip 0X if it appears

//如果是0x開(kāi)頭的,那么截取字符串,字符串從索引為2的位置開(kāi)始,一直到末尾

if ([cString hasPrefix:@"0X"])

cString = [cString substringFromIndex:2];

//如果是#開(kāi)頭的,那么截取字符串,字符串從索引為1的位置開(kāi)始,一直到末尾

if ([cString hasPrefix:@"#"])

cString = [cString substringFromIndex:1];

if ([cString length] != 6)

return [UIColor clearColor];

// Separate into r, g, b substrings

NSRange range;

range.location = 0;

range.length = 2;

//r

NSString *rString = [cString substringWithRange:range];

//g

range.location = 2;

NSString *gString = [cString substringWithRange:range];

//b

range.location = 4;

NSString *bString = [cString substringWithRange:range];

// Scan values

unsigned int r, g, b;

[[NSScanner scannerWithString:rString] scanHexInt:&r];

[[NSScanner scannerWithString:gString] scanHexInt:&g];

[[NSScanner scannerWithString:bString] scanHexInt:&b];

return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];

}

@end

使用:

self.view.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8" alpha:1.0];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1、不想每次都除255.0 + (UIColor *)colorWithR:(CGFloat)red G:(CGF...
    Moker_C閱讀 305評(píng)論 0 1
  • iOS中的顏色有UIColor、CGColor、CIColor三種,下面對(duì)三種顏色分別進(jìn)行說(shuō)明: 一、常用的UIC...
    lfp901020閱讀 6,731評(píng)論 0 7
  • 1. 打印View所有子視圖 po [[self view]recursiveDescription] 2. la...
    浩成哥哥閱讀 807評(píng)論 0 0
  • 我喜歡按部就班、循規(guī)蹈矩的生活。所以我喜歡制定各種計(jì)劃:年計(jì)劃、月計(jì)劃、周計(jì)劃、日計(jì)劃、課時(shí)計(jì)劃。當(dāng)生活和工...
    鹿鳴閣主閱讀 307評(píng)論 2 2
  • 寫第一篇文章的時(shí)候 還是年初 現(xiàn)在已經(jīng)到了年末了。來(lái)不及分享這些點(diǎn)點(diǎn)滴滴 現(xiàn)實(shí)又在拼命趕路。人生短短幾十年 與誰(shuí)相...
    卉卉在哪里閱讀 160評(píng)論 0 0