Skip to content

Instantly share code, notes, and snippets.

View cxa's full-sized avatar
🦥

realazy cxa

🦥
View GitHub Profile
- (void)drawRect:(NSRect)rect
{
// Drawing code here.
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
NSAttributedString *str = [[NSAttributedString alloc ]
initWithString:@"学习 Core Text. Learning Core Text. 中华人民共和国。"
attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Adobe Song Std" size:24.0f], (NSString *)kCTFontAttributeName, [NSNumber numberWithBool:YES], (NSString *)kCTVerticalFormsAttributeName, nil]];
CFAttributedStringRef attrString = (CFAttributedStringRef)str;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
- (NSString *)convertISBN10To13:(NSString *)isbn10
{
NSString *prefix = [NSString stringWithFormat:@"978%@", [isbn10 substringToIndex:9]];
NSInteger sum = 0;
for (int i=0; i<12; i++){
char c = [prefix characterAtIndex:i];
int n = c - '0';
int w = (i % 2) ? 3 : 1;
sum += w * n;
}
@cxa
cxa / Printing
Created November 12, 2010 16:58
iOS 4.2 devices can print wirelessly only to printers that support AirPrint and are running the latest available firmware. Some currently available printers that support AirPrint are:
- HP Photosmart Premium Fax e-All-in-One Printer - C410
- HP Photosmart Premium e-All-in-One Printer series - C310
- HP Photosmart Plus e-All-in-One Printer series - B210
- HP ENVY 100 e-All-in-One Printer Series - D410
- HP Photosmart eStation Printer series - C510
=NEW FEATURES=
- Ready for multitasking and Retina Display
- Print page (iOS 4.2 required)
- Image Gallery for current page
- Share text, image or URL to Delicious, Facebook, Google Reader, Instapaper, Pinboard, Read It Later, Tumblr and/or Twitter
- Add code snippet font size adjustment in Reading Options
=ENHANCEMENTS=
- New image viewer, similar to Photo.app's flavor
- Improved 'Find in Page', similar to desktop browser
@cxa
cxa / toChineseNumer.m
Created May 28, 2011 12:46
Convert Arabic number to Chinese
#define CHAR_MAX 32
NSString* toChineseNumer(NSUInteger num){
static NSString *cnums = @"〇一二三四五六七八九";
unichar ch[32];
int i = 0;
do ch[i++] = [cnums characterAtIndex:num%10];
while ((num /= 10) > 0);
@cxa
cxa / NSUserDefaults
Created September 12, 2011 02:22
NSUserDefaults shortcut
#define DEFAULTS(type, key) ([[NSUserDefaults standardUserDefaults] type##ForKey:key])
#define SET_DEFAULTS(Type, key, val) do {\
[[NSUserDefaults standardUserDefaults] set##Type:val forKey:key];\
[[NSUserDefaults standardUserDefaults] synchronize];\
} while (0)
@cxa
cxa / gist:2437310
Created April 21, 2012 14:17
CTFont with kCTFontCascadeListAttribute
CGFloat pointSize = 17.0;
CTFontDescriptorRef descLatin = CTFontDescriptorCreateWithNameAndSize(CFSTR("TimesNewRomanPSMT"), pointSize);
CTFontDescriptorRef descZh = CTFontDescriptorCreateWithNameAndSize(CFSTR("STHeitiSC-Light"), pointSize);
NSArray *cascade = [NSArray arrayWithObjects:(id)descLatin, (id)descZh, nil];
NSDictionary *attrs = [NSDictionary dictionaryWithObject:cascade forKey:(id)kCTFontCascadeListAttribute];
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attrs);
CTFontRef font = CTFontCreateWithFontDescriptor(desc, pointSize, NULL);
// use the font
...
CFRelease(descLatin);
@cxa
cxa / CGFontToFontData.m
Created May 5, 2012 15:06 — forked from Bokugene/CGFontToFontData.m
Read Table Data from a CGFont, then wrap them into a OTF/TTF font.
typedef struct FontHeader {
int32_t fVersion;
uint16_t fNumTables;
uint16_t fSearchRange;
uint16_t fEntrySelector;
uint16_t fRangeShift;
}FontHeader;
typedef struct TableEntry {
uint32_t fTag;
@cxa
cxa / cascadelist.m
Created May 11, 2012 08:19 — forked from jjgod/cascadelist.m
Use cascade list attribute to customize font fallback in Core Text
#import <ApplicationServices/ApplicationServices.h>
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 0;
NSAutoreleasePool *pool = [NSAutoreleasePool new];
CFStringRef name = (CFStringRef) [NSString stringWithUTF8String: argv[1]];
@cxa
cxa / gist:3936832
Created October 23, 2012 05:18 — forked from huacnlee/gist:3936299
Mac 清理 TextMate 2 不断升级带出一堆多余的打开方式
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user