Skip to content

Instantly share code, notes, and snippets.

@slow-coding
Last active November 3, 2023 22:19
Show Gist options
  • Save slow-coding/5424693 to your computer and use it in GitHub Desktop.
Save slow-coding/5424693 to your computer and use it in GitHub Desktop.
An Objective-C class with useful macros and functions
//
// DZUtility.h
// Utility
//
// Created by Darren Zheng on 13-4-20.
// Copyright (c) 2013年 Darren Zheng. All rights reserved.
//
#import <Foundation/Foundation.h>
//////////////////////////////////////////////////////////////////
// Debug/Release
//////////////////////////////////////////////////////////////////
#ifdef DEBUG
// Debug Mode
//...
#else
// Release Mode
//...
// Clean NSLog
#define NSLog(...) {};
#endif
//////////////////////////////////////////////////////////////////
// Simulator/Device
//////////////////////////////////////////////////////////////////
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
//////////////////////////////////////////////////////////////////
// ARC/no ARC
//////////////////////////////////////////////////////////////////
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
//////////////////////////////////////////////////////////////////
// Abbreviation
//////////////////////////////////////////////////////////////////
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define SharedApplication [UIApplication sharedApplication]
#define MainBundle [NSBundle mainBundle]
#define MainScreen [UIScreen mainScreen]
//////////////////////////////////////////////////////////////////
// UI
//////////////////////////////////////////////////////////////////
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
//////////////////////////////////////////////////////////////////
// System
//////////////////////////////////////////////////////////////////
#define SystemVersion ([[[UIDevice currentDevice] systemVersion] floatValue])
#define IsRetina ([[UIScreen mainScreen] scale]==2)
#define IsiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define IsiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IsAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
#define IsAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
#define IsAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
#define AppName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
//////////////////////////////////////////////////////////////////
// CGRect
//////////////////////////////////////////////////////////////////
#define SetX(v, x) v.frame = CGRectMake(x, v.frame.origin.y, v.frame.size.width, v.frame.size.height)
#define SetY(v, y) v.frame = CGRectMake(v.frame.origin.x, y, v.frame.size.width, v.frame.size.height)
#define SetWidth(v, w) v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, w, v.frame.size.height)
#define SetHeight(v, h) v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, h)
#define SetOrigin(v, x, y) v.frame = CGRectMake(x, y, v.frame.size.width, v.frame.size.height)
#define SetSize(v, w, h) v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, w, h)
#define SetFrame(v, x, y, w, h) v.frame = CGRectMake(x, y, w, h)
#define AddX(v, offset) v.frame = CGRectMake(v.frame.origin.x + offset, v.frame.origin.y, v.frame.size.width, v.frame.size.height)
#define AddY(v, offset) v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y + offset, v.frame.size.width, v.frame.size.height)
#define X(v) v.frame.origin.x
#define Y(v) v.frame.origin.y
#define Width(v) v.frame.size.width
#define Height(v) v.frame.size.height
#define Origin(v) v.frame.origin
#define Size(v) v.frame.size
//////////////////////////////////////////////////////////////////
// Color
//////////////////////////////////////////////////////////////////
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
//////////////////////////////////////////////////////////////////
// Format
//////////////////////////////////////////////////////////////////
@interface DZUtility : NSObject
void dail(NSString *strNum);
void openURL(NSString *strURL);
BOOL canOpenURL(NSString *strURL);
void mailTo(NSString *strMailAddr);
void showNetworkActivityIndicator(BOOL bShow);
void ignoreTouch(BOOL bIgnore);
NSString *documentsDirectory();
@end
//
// DZUtility.m
// Utility
//
// Created by Darren Zheng on 13-4-20.
// Copyright (c) 2013年 Darren Zheng. All rights reserved.
//
#import "DZUtility.h"
@implementation DZUtility
void dail(NSString *strNum)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", strNum]]];
}
void openURL(NSString *strURL)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strURL]];
}
BOOL canOpenURL(NSString *strURL)
{
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strURL]];
}
void mailTo(NSString *strMailAddr)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@", strMailAddr]]];
}
void showNetworkActivityIndicator(BOOL bShow)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = bShow;
}
void ignoreTouch(BOOL bIgnore)
{
if(bIgnore)
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}
else
{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
}
NSString *documentsDirectory()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment