Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created February 17, 2011 14:28
Show Gist options
  • Save marshluca/831816 to your computer and use it in GitHub Desktop.
Save marshluca/831816 to your computer and use it in GitHub Desktop.
MUtility.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface MUtility : NSObject {
}
/**获取随机数**/
+ (int) randomNumber;
/**随机获取颜色**/
+ (UIColor *) randomColor;
/**获取Documents下的文件路径**/
+ (NSString *) documentPathForFile:(NSString *)fileName;
/**计算字符串的高度**/
+ (CGFloat)getStringHeight:(NSString *)string font:(UIFont *)font;
/**读取本地数据到一个数组变量中**/
+ (NSMutableArray *) getDataArrayFromFile:(NSString *)fileName;
@end
#import "MUtility.h"
@implementation MUtility
+ (int)randomNumber
{
srandom(time(NULL));
return random()%100 +1;
}
+ (UIColor *)randomColor
{
static BOOL seeded = NO;
if (!seeded) {
seeded = YES;
srandom(time(NULL));
}
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
+ (CGFloat)getStringHeight:(NSString *)string font:(UIFont *)font {
CGSize size = [string sizeWithFont:font
constrainedToSize:CGSizeMake(300.0f, 1000000.0f)
lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
+ (NSString *)documentPathForFile:(NSString *)fileName
{
// NSLog(@"documents: %@",NSHomeDirectory());
return [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",fileName]];
}
+ (NSMutableArray *) getDataArrayFromFile:(NSString *)fileName
{
NSString *filePath = [MUtility documentPathForFile:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
NSMutableArray *initArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[initArray writeToFile:filePath atomically:YES];
}
return [[NSMutableArray alloc] initWithContentsOfFile:filePath];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment