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:4450810
Last active December 10, 2015 14:58
Plist文件操作
/*
先在项目中加好默认的设置,读取的时候如果没有就把默认的写入。
*/
#define FILE_NAME @"CONFIG"
#define FILE_TYPE @"plist"
// eg: cName shanghai
+ (void)setDefultCity:(NSString *)cName
{
@xuyunan
xuyunan / gist:4481564
Created January 8, 2013 05:50
导航线路 (系统地图)
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng) addressDictionary:nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:place];
destination.name = addrName;
NSArray *items = @[destination];
NSDictionary* options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
[MKMapItem openMapsWithItems:items launchOptions:options];
} else {
@xuyunan
xuyunan / gist:4500489
Created January 10, 2013 08:34
随机数
// 产生随机数种子
srandom((unsigned)time(NULL));
- (CGPoint)randomPoint
{
CGPoint result;
NSRect r = [self bounds];
result.x = r.origin.x + random()%(int)r.size.width;
result.y = r.origin.y + random()%(int)r.size.height;
return result;
@xuyunan
xuyunan / gist:4562677
Last active December 11, 2015 06:58
代码统计
cd到相应目录,在终端中输入下面两个命令,输出结果相加
find ./ -name "*.m" -exec cat {} \; |wc -l
find ./ -name "*.h" -exec cat {} \; | wc -l
@xuyunan
xuyunan / gist:4633067
Last active December 11, 2015 17:18
十六进制转UIColor
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// from http://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
@xuyunan
xuyunan / gist:4661667
Created January 29, 2013 03:54
iOS5以上修改导航条上字体颜色
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:navBarTitleColor forKey:UITextAttributeTextColor];
[titleBarAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.f, 0.f)] forKey:UITextAttributeTextShadowOffset];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UINavigationBar appearance] setTintColor:kNAVIGATIONBAR_BARBUTTON_COLOR];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],
UITextAttributeTextColor,
@xuyunan
xuyunan / gist:4662711
Last active December 11, 2015 21:28
自定义annotation的动画
// 从上往下落的动作 (MKMapViewDelegate method)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
CGRect visibleRect = [mapView annotationVisibleRect];
for (MKAnnotationView *view in views) {
CGRect endFrame = view.frame;
CGRect startFrame = endFrame;
startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
view.frame = startFrame;
[UIView beginAnimations:@"drop" context:NULL];
@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])
{
@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: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";