記錄每一點(diǎn)一滴。
...
2017年02月25日(擴(kuò)展)
hexColor
.h文件
+ (UIColor *) hexColorWithString: (NSString *)str;
.m文件
+ (UIColor *) hexColorWithString: (NSString *)str
{
NSString *cString = [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 charactersif ([cString length] < 6) return [UIColor blackColor];
// strip 0X if it appearsif ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6) return [UIColor blackColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
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:1.0f];
}
iOS中通過color創(chuàng)建一張純色的圖片并且可以設(shè)置大惺竟:
.h文件 (通過創(chuàng)建UIImage的分類實(shí)現(xiàn)方法)
/**
* 通過size生成圖片
**/
+ (UIImage *)createImageWithSize:(CGSize)size color:(UIColor *)color;
.m文件
+ (UIImage *)createImageWithSize:(CGSize)size color:(UIColor *)color
{
// 準(zhǔn)備繪制環(huán)境
UIGraphicsBeginImageContext(size);
// 獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 設(shè)置顏色
CGContextSetFillColorWithColor(context, [color CGColor]);
// 設(shè)置渲染范圍
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
// 取得圖片
UIImage *colorImg = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束繪制
UIGraphicsEndImageContext();
return colorImg;
}
通過字體大小計(jì)算NSString的size(這里未考慮其他attributes)
.h文件 (通過NSString分類的方式實(shí)現(xiàn))
/**
*返回值是該字符串所占的size(大小)
*font:傳入的字符串所用的字體(字體大小不一樣,顯示出來的面積也不同)
*maxSize:CGSizeMake(MAXFLOAT,MAXFLOAT)
*/
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize;
.m文件
/// 實(shí)現(xiàn)方式
-(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
2017年02月26日更新工具類(繼承自NSObject)
獲取隨機(jī)數(shù)并且可以設(shè)置范圍
+ (int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to - from + 1)));
}
通過文件名獲取路徑
/// 根據(jù)文件名來獲取文件路徑(document路徑)
+ (NSString *)dataFilePath:(NSString *)fileName
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:fileName];
}