Cocoa Snippets

iPhone and Mac code

April 29, 2011 at 5:05pm
0 notes

UIView with shadow

tableView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:1].CGColor;

tableView.layer.masksToBounds = NO;

tableView.layer.shadowOffset = CGSizeMake(0, 1);

tableView.layer.shadowRadius = 10;

tableView.layer.shadowOpacity = .65f;

April 25, 2011 at 3:32pm
2 notes

iPhone List of fonts for iOS SDK 4.3

Family name: Thonburi

        Font name: Thonburi-Bold

        Font name: Thonburi

Family name: Snell Roundhand

        Font name: SnellRoundhand-Bold

        Font name: SnellRoundhand

Family name: Academy Engraved LET

        Font name: AcademyEngravedLetPlain

Family name: Marker Felt

        Font name: MarkerFelt-Wide

        Font name: MarkerFelt-Thin

Family name: Geeza Pro

        Font name: GeezaPro-Bold

        Font name: GeezaPro

Family name: Arial Rounded MT Bold

        Font name: ArialRoundedMTBold

Family name: Trebuchet MS

        Font name: TrebuchetMS

        Font name: TrebuchetMS-Bold

        Font name: TrebuchetMS-Italic

        Font name: Trebuchet-BoldItalic

Family name: Arial

        Font name: Arial-BoldMT

        Font name: ArialMT

        Font name: Arial-ItalicMT

        Font name: Arial-BoldItalicMT

Family name: Gurmukhi MN

        Font name: GurmukhiMN

        Font name: GurmukhiMN-Bold

Family name: Malayalam Sangam MN

        Font name: MalayalamSangamMN-Bold

        Font name: MalayalamSangamMN

Family name: Bradley Hand

        Font name: BradleyHandITCTT-Bold

Family name: Kannada Sangam MN

        Font name: KannadaSangamMN

        Font name: KannadaSangamMN-Bold

Family name: Bodoni 72 Oldstyle

        Font name: BodoniSvtyTwoOSITCTT-Book

        Font name: BodoniSvtyTwoOSITCTT-Bold

        Font name: BodoniSvtyTwoOSITCTT-BookIt

Family name: Cochin

        Font name: Cochin

        Font name: Cochin-BoldItalic

        Font name: Cochin-Italic

        Font name: Cochin-Bold

Family name: Sinhala Sangam MN

        Font name: SinhalaSangamMN

        Font name: SinhalaSangamMN-Bold

Family name: Hiragino Kaku Gothic ProN

        Font name: HiraKakuProN-W6

        Font name: HiraKakuProN-W3

Family name: Papyrus

        Font name: Papyrus

Family name: Verdana

        Font name: Verdana

        Font name: Verdana-Bold

        Font name: Verdana-BoldItalic

        Font name: Verdana-Italic

Family name: Zapf Dingbats

        Font name: ZapfDingbatsITC

Family name: Courier

        Font name: Courier-Bold

        Font name: Courier

        Font name: Courier-BoldOblique

        Font name: Courier-Oblique

Family name: Hoefler Text

        Font name: HoeflerText-Black

        Font name: HoeflerText-Italic

        Font name: HoeflerText-Regular

        Font name: HoeflerText-BlackItalic

Family name: Helvetica

        Font name: Helvetica

        Font name: Helvetica-Oblique

        Font name: Helvetica-BoldOblique

        Font name: Helvetica-Bold

Family name: Hiragino Mincho ProN

        Font name: HiraMinProN-W3

        Font name: HiraMinProN-W6

Family name: Bodoni Ornaments

        Font name: BodoniOrnamentsITCTT

Family name: Apple Color Emoji

        Font name: AppleColorEmoji

Family name: Optima

        Font name: Optima-Italic

        Font name: Optima-Regular

        Font name: Optima-BoldItalic

        Font name: Optima-Bold

Family name: Gujarati Sangam MN

        Font name: GujaratiSangamMN

        Font name: GujaratiSangamMN-Bold

Family name: Devanagari Sangam MN

        Font name: DevanagariSangamMN

        Font name: DevanagariSangamMN-Bold

Family name: Times New Roman

        Font name: TimesNewRomanPS-ItalicMT

        Font name: TimesNewRomanPS-BoldMT

        Font name: TimesNewRomanPSMT

        Font name: TimesNewRomanPS-BoldItalicMT

Family name: Kailasa

        Font name: Kailasa

        Font name: Kailasa-Bold

Family name: Telugu Sangam MN

        Font name: TeluguSangamMN-Bold

        Font name: TeluguSangamMN

Family name: Heiti SC

        Font name: STHeitiSC-Medium

        Font name: STHeitiSC-Light

Family name: Futura

        Font name: Futura-Medium

        Font name: Futura-CondensedExtraBold

        Font name: Futura-MediumItalic

Family name: Bodoni 72

        Font name: BodoniSvtyTwoITCTT-BookIta

        Font name: BodoniSvtyTwoITCTT-Book

        Font name: BodoniSvtyTwoITCTT-Bold

Family name: Baskerville

        Font name: Baskerville-Bold

        Font name: Baskerville-Italic

        Font name: Baskerville-BoldItalic

        Font name: Baskerville

Family name: Chalkboard SE

        Font name: ChalkboardSE-Regular

        Font name: ChalkboardSE-Bold

Family name: Heiti TC

        Font name: STHeitiTC-Medium

        Font name: STHeitiTC-Light

Family name: Copperplate

        Font name: Copperplate

        Font name: Copperplate-Bold

Family name: Party LET

        Font name: PartyLetPlain

Family name: American Typewriter

        Font name: AmericanTypewriter-Bold

        Font name: AmericanTypewriter

Family name: AppleGothic

        Font name: AppleGothic

Family name: Bangla Sangam MN

        Font name: BanglaSangamMN-Bold

        Font name: BanglaSangamMN

Family name: Zapfino

        Font name: Zapfino

Family name: Tamil Sangam MN

        Font name: TamilSangamMN

        Font name: TamilSangamMN-Bold

Family name: DB LCD Temp

        Font name: DBLCDTempBlack

Family name: Arial Hebrew

        Font name: ArialHebrew

        Font name: ArialHebrew-Bold

Family name: Heiti K

        Font name: STHeitiK-Medium

        Font name: STHeitiK-Light

Family name: Georgia

        Font name: Georgia-Italic

        Font name: Georgia-BoldItalic

        Font name: Georgia-Bold

        Font name: Georgia

Family name: Heiti J

        Font name: STHeitiJ-Medium

        Font name: STHeitiJ-Light

Family name: Helvetica Neue

        Font name: HelveticaNeue-Bold

        Font name: HelveticaNeue

        Font name: HelveticaNeue-BoldItalic

        Font name: HelveticaNeue-Italic

Family name: Gill Sans

        Font name: GillSans-BoldItalic

        Font name: GillSans-Italic

        Font name: GillSans

        Font name: GillSans-Bold

Family name: Chalkduster

        Font name: Chalkduster

Family name: Palatino

        Font name: Palatino-Roman

        Font name: Palatino-Bold

        Font name: Palatino-BoldItalic

        Font name: Palatino-Italic

Family name: Courier New

        Font name: CourierNewPSMT

        Font name: CourierNewPS-BoldMT

        Font name: CourierNewPS-BoldItalicMT

        Font name: CourierNewPS-ItalicMT

Family name: Oriya Sangam MN

        Font name: OriyaSangamMN-Bold

        Font name: OriyaSangamMN

Family name: Didot

        Font name: Didot-Italic

        Font name: Didot

        Font name: Didot-Bold

Family name: Bodoni 72 Smallcaps

        Font name: BodoniSvtyTwoSCITCTT-Book

April 10, 2011 at 9:44pm
0 notes
Subtitles Wizard
Downloading subtitles has never been easier :)
at Mac App Store.

Subtitles Wizard

Downloading subtitles has never been easier :)

at Mac App Store.

March 17, 2011 at 10:53am
0 notes

Remove scrollview bounce from UIWebView

for (id subview in webView.subviews)

if ([[subview class] isSubclassOfClass: [UIScrollView class]])

((UIScrollView *)subview).bounces = NO;

March 16, 2011 at 11:14am
0 notes

Remove HTML tags from a NSString

NSRange r;

NSString *strWithoutHTMLTags = [[str copy] autorelease];

while ((r = [strWithoutHTMLTags rangeOfString:@”<[^>]+>” options:NSRegularExpressionSearch]).location != NSNotFound)

strWithoutHTMLTags = [strWithoutHTMLTags stringByReplacingCharactersInRange:r withString:@”“];

March 14, 2011 at 10:06am
1 note

UINavigationController pushViewController from landscape to portrait

Little hack…

At portrait view controller

- (void)viewDidLoad {

[super viewDidLoad];

if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {

UIViewController *viewController = [[UIViewController alloc] init];

[self presentModalViewController:viewController animated:NO];

[self dismissModalViewControllerAnimated:NO];

[viewController release];

}

}

March 2, 2011 at 8:31am
0 notes

CALayer from UIImage

- (CALayer *)layerFromImageNamed:(NSString *)name { CALayer *imgLayer = [[CALayer alloc] init]; imgLayer.backgroundColor = [UIColor clearColor].CGColor; imgLayer.contentsGravity = kCAGravityResizeAspect; UIImage *img = [UIImage imageNamed:name]; imgLayer.contents = (id)img.CGImage; [imgLayer setFrame:CGRectMake(0, 0, img.size.width, img.size.height)]; return imgLayer; }

February 17, 2011 at 10:24am
0 notes

UITabBar background

CGRect frame = CGRectMake(0, 0, 480, 49);

    UIView *v = [[UIView alloc] initWithFrame:frame];

    UIImage *i = [UIImage imageNamed:@”redGradient.png”];

    UIColor *c = [[UIColor alloc] initWithPatternImage:i];

    v.backgroundColor = c;

    [c release];

    [tabBar insertSubview:v atIndex:0];

    [v release];

//or with color

UIColor *color = UIColorFromRGB(0xF42626);

CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);

    UIView *v = [[UIView alloc] initWithFrame:frame];

    [v setBackgroundColor:color];

    [v setAlpha:0.5];

    [tabBar insertSubview:v atIndex:0];

    [v release];

January 25, 2011 at 9:19am
16 notes

Nice NSLog debugging

NSLog(@”%s %d %s”, __FILE__, __LINE__, __PRETTY_FUNCTION__);

January 11, 2011 at 1:50pm
4 notes

Force autorotation

[[UIDevice currentDevice] setOrientation:[[UIDevice currentDevice] orientation]];

January 10, 2011 at 9:13am
12 notes

Singletons

+ (object*)sharedInstance

{

  static object* sharedInstance = nil;

  @synchronized (self) {

  if (sharedInstance == nil)

sharedInstance = [[object* alloc] init];

}

return sharedInstance;

}

December 23, 2010 at 9:56am
0 notes

Using XML

If you can ignore a little loss of performance and need a complete XML control with many output options, I recommend http://code.google.com/p/kissxml/

December 22, 2010 at 12:31pm
1 note

XML to NSDictionary

From: http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

//
// XMLReader.h
//

#import <Foundation/Foundation.h>

@interface XMLReader : NSObject
{
    NSMutableArray *dictionaryStack;
    NSMutableString *textInProgress;
    NSError **errorPointer;
}

+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer;
+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer;

@end


//// XMLReader.m//

#import "XMLReader.h"

NSString *const kXMLReaderTextNodeKey = @”text”;

@interface XMLReader (Internal)

- (id)initWithError:(NSError **)error;- (NSDictionary *)objectWithData:(NSData *)data;

@end

@implementation XMLReader

#pragma mark -#pragma mark Public methods

+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error{    XMLReader *reader = [[XMLReader allocinitWithError:error];    NSDictionary *rootDictionary = [reader objectWithData:data];    [reader release];    return rootDictionary;}

+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error{    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];    return [XMLReader dictionaryForXMLData:data error:error];}

#pragma mark -#pragma mark Parsing

- (id)initWithError:(NSError **)error{    if (self = [super init])    {        errorPointer = error;    }    return self;}

- (void)dealloc{    [dictionaryStack release];    [textInProgress release];    [super dealloc];}

- (NSDictionary *)objectWithData:(NSData *)data{    // Clear out any old data    [dictionaryStack release];    [textInProgress release];        dictionaryStack = [[NSMutableArray allocinit];    textInProgress = [[NSMutableString allocinit];        // Initialize the stack with a fresh dictionary    [dictionaryStack addObject:[NSMutableDictionary dictionary]];        // Parse the XML    NSXMLParser *parser = [[NSXMLParser allocinitWithData:data];    parser.delegate = self;    BOOL success = [parser parse];        // Return the stack’s root dictionary on success    if (success)    {        NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];        return resultDict;    }        return nil;}

#pragma mark -#pragma mark NSXMLParserDelegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{    // Get the dictionary for the current level in the stack    NSMutableDictionary *parentDict = [dictionaryStack lastObject];        // Create the child dictionary for the new element, and initilaize it with the attributes    NSMutableDictionary *childDict = [NSMutableDictionary dictionary];    [childDict addEntriesFromDictionary:attributeDict];        // If there’s already an item for this key, it means we need to create an array    id existingValue = [parentDict objectForKey:elementName];    if (existingValue)    {        NSMutableArray *array = nil;        if ([existingValue isKindOfClass:[NSMutableArray class]])        {            // The array exists, so use it            array = (NSMutableArray *) existingValue;        }        else        {            // Create an array if it doesn’t exist            array = [NSMutableArray array];            [array addObject:existingValue];                        // Replace the child dictionary with an array of children dictionaries            [parentDict setObject:array forKey:elementName];        }                // Add the new child dictionary to the array        [array addObject:childDict];    }    else    {        // No existing value, so update the dictionary        [parentDict setObject:childDict forKey:elementName];    }        // Update the stack    [dictionaryStack addObject:childDict];}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{    // Update the parent dict with text info    NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];        // Set the text property    if ([textInProgress length> 0)    {        // Get rid of leading + trailing whitespace        [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];                // Reset the text        [textInProgress release];        textInProgress = [[NSMutableString allocinit];    }        // Pop the current dict    [dictionaryStack removeLastObject];}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{    // Build the text value    [textInProgress appendString:string];}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{    // Set the error pointer to the parser’s error object    *errorPointer = parseError;}

@end

December 13, 2010 at 2:36pm
1 note

Change FaceBook share layout to fit UIWebView with JavaScript

[webView stringByEvaluatingJavaScriptFromString:

@”var metadata = document.createElement(‘meta’);”

"metadata.name = ‘viewport’;"

"metadata.content = ‘width=320’;"

"document.getElementsByTagName(‘head’)[0].appendChild(metadata);"

//margin top to text field, or else it would be behind photo

“document.getElementById(‘UIShareComposer_MessageLabel’).style.margin = ‘55px 0 0 0’;”];

 

[webView setScalesPageToFit:NO];

1:37pm
0 notes

UIWebView Page Width by JavaScript

[webView stringByEvaluatingJavaScriptFromString:

@”var metadata = document.createElement(‘meta’);”

"metadata.name = ‘viewport’;"

"metadata.content = ‘width=320’;"

"document.getElementsByTagName(‘head’)[0].appendChild(metadata);"];

[webView setScalesPageToFit:YES];