Skip to content

Instantly share code, notes, and snippets.

View xuyunan's full-sized avatar
🎯
Focusing

Tommy xuyunan

🎯
Focusing
View GitHub Profile
@xuyunan
xuyunan / gist:7e01d0bab39630b03caa
Created August 21, 2015 07:00
iOS所有字体
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
@xuyunan
xuyunan / gist:c72626bca1cd05731aae
Last active August 29, 2015 14:07
计算文本高度
- (CGFloat)heighWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
return [self sizeWithFont:font constrainedToSize:size].height;
}
- (CGFloat)widthWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
return [self sizeWithFont:font constrainedToSize:size].width;
}
@xuyunan
xuyunan / gist:1312143d1d2cd8d44705
Last active August 29, 2015 14:04
计算缓存大小,清楚缓存
// remove
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachePath isDirectory:YES]) {
connsuccess = [fileManager removeItemAtPath:cachePath error:nil];
}
// size bytes
- (unsigned long long)sizeForPath:(NSString *)path
{
__block unsigned long long size = 0;
@xuyunan
xuyunan / gist:f29e5883126786f9bf2e
Last active August 29, 2015 14:04
字符个数限制
// - (void)viewDidAppear
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldTextDidChangeNotification:)
name:UITextFieldTextDidChangeNotification
object:nil];
// - (void)viewDidDisappear
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidChangeNotification
object:nil];
// 截全屏
- (UIImage *)imageWithScreenContents
{
UIGraphicsBeginImageContext(SCREEN_SIZE);
UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aImage;
}
@xuyunan
xuyunan / gist:5683076
Created May 31, 2013 05:27
统计字数
+ (int)countWord:(NSString *)s
{
int i,n=[s length],l=0,a=0,b=0;
unichar c;
for(i=0;i<n;i++){
c=[s characterAtIndex:i];
if(isblank(c)){
b++;
}else if(isascii(c)){
a++;
@xuyunan
xuyunan / gist:5401245
Created April 17, 2013 02:02
创建用户目录
+ (NSString *)dirUserData:(NSString *)userid
{
NSArray *arrayFiles = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [arrayFiles objectAtIndex:0];
NSString *userDataDir = [docPath stringByAppendingPathComponent:userid];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL isExist = [fileManager fileExistsAtPath:userDataDir isDirectory:&isDirectory];
if (!isExist || (isExist && !isDirectory)) {
NSError *error;
@xuyunan
xuyunan / gist:5219053
Created March 22, 2013 05:01
创建常量
// Easiest way:
// Prefs.h
#define PREFS_MY_CONSTANT @"prefs_my_constant"
// Better way:
// Prefs.h
extern NSString * const PREFS_MY_CONSTANT;
// Prefs.m
NSString * const PREFS_MY_CONSTANT = @"prefs_my_constant";
@xuyunan
xuyunan / gist:5211725
Created March 21, 2013 09:13
创建单例
static dispatch_once_t predicate;
+ (id)sharedUser
{
static id shareUser = nil;
dispatch_once(&predicate, ^{
shareUser = [[super allocWithZone:NULL] init];
});
return shareUser;
}
@xuyunan
xuyunan / gist:5177077
Last active December 15, 2015 00:59
发送短信
// iOS4.0以上
// MessageUI.framework
// #import <MessageUI/MessageUI.h>
// @interface ViewController () <MFMessageComposeViewControllerDelegate>
- (IBAction)sendSMS:(id)sender
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{