Created
May 30, 2011 09:15
-
-
Save iley/998639 to your computer and use it in GitHub Desktop.
ios utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Utilities.h" | |
NSString *filePath(NSString *file) { | |
return [[NSBundle mainBundle] pathForResource:[file stringByDeletingPathExtension] ofType:[file pathExtension]]; | |
} | |
NSString* writablePath(NSString* file) { | |
NSFileManager *fm = [NSFileManager defaultManager]; | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *writablePath = [documentsDirectory stringByAppendingFormat:@"/%@",file]; | |
if(![fm fileExistsAtPath:writablePath]) { | |
if(![fm copyItemAtPath:filePath(file) toPath:writablePath error:nil]) { | |
NSLog(@"Error copying '%@' to '%@'",filePath(file),writablePath); | |
} | |
} | |
return writablePath; | |
} | |
NSURL* fileURL(NSString *file) { | |
return [[NSBundle mainBundle] URLForResource:[file stringByDeletingPathExtension] withExtension:[file pathExtension]]; | |
} | |
NSString *fileContent(NSString *file) { | |
return [NSString stringWithContentsOfFile:filePath(file) encoding:NSUTF8StringEncoding error:nil]; | |
} | |
NSString *fileContentWithEnc(NSString *file, NSString *encName) { | |
return [NSString stringWithContentsOfFile:filePath(file) encoding:encodingByName(encName) error:nil]; | |
} | |
NSStringEncoding encodingByName(NSString *name) { | |
return CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)name)); | |
} | |
void loadLocalFile(UIWebView *webView, NSString *fileName) { | |
NSString *path = filePath(fileName); | |
NSURL *url = fileURL(fileName); | |
NSData *htmlData = [NSData dataWithContentsOfFile:path]; | |
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:url]; | |
} | |
void loadLocalFileWithEnc(UIWebView *webView, NSString *fileName, NSString *enc) { | |
NSString *path = filePath(fileName); | |
NSURL *url = fileURL(fileName); | |
NSData *htmlData = [NSData dataWithContentsOfFile:path]; | |
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:enc baseURL:url]; | |
} | |
void disableBouncing(UIWebView *webView) { | |
for (id subview in webView.subviews) | |
if ([[subview class] isSubclassOfClass: [UIScrollView class]]) | |
((UIScrollView *)subview).bounces = NO; | |
} | |
id cutPrefix(NSString *string, NSString *prefix) { | |
if ([string hasPrefix:prefix]) { | |
return [string substringFromIndex:[prefix length]]; | |
} | |
return nil; | |
} | |
int sliderPosition(UISlider* slider) { | |
return slider.frame.origin.x + slider.frame.size.width * slider.value / (slider.maximumValue - slider.minimumValue); | |
} | |
NSArray *lines(NSString *str) { | |
return [[str stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\r"]]; | |
} | |
void disableInput(UIWebView *webView) { | |
UIScrollView *webViewContentView; | |
for (UIView *checkView in [webView subviews] ) { | |
if ([checkView isKindOfClass:[UIScrollView class]]) { | |
webViewContentView = (UIScrollView*)checkView; | |
break; | |
} | |
} | |
for (UIView *checkView in [webViewContentView subviews] ) { | |
checkView.userInteractionEnabled = NO; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment