Skip to content

Instantly share code, notes, and snippets.

View troyharris's full-sized avatar

Troy Harris troyharris

View GitHub Profile
@troyharris
troyharris / gist:5891542
Created June 29, 2013 15:26
Methods for getting the real height and width of a device based on orientation
+(CGFloat)getRealDeviceWidth {
CGFloat deviceWidth;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsLandscape(orientation)) {
deviceWidth = [UIScreen mainScreen].bounds.size.height;
} else {
deviceWidth = [UIScreen mainScreen].bounds.size.width;
}
return deviceWidth;
}
@troyharris
troyharris / gist:5891570
Created June 29, 2013 15:35
Get the center of the view regardless of orientation: x is always horizontal and y is always vertical based on the current rotation
+(CGPoint)getViewRealCenter:(UIView *)view {
CGPoint realCenter;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
realCenter = UIInterfaceOrientationIsLandscape(orientation) ? CGPointMake(view.center.y, view.center.x) : view.center;
return realCenter;
}
// THGridMenuItem+ProjectItem.h
#import "THGridMenuItem.h"
#import "Project.h"
@interface THGridMenuItem (ProjectItem)
@property (nonatomic, strong) Project *project;
-(Project *)project;
-(void)setProject:(Project *)project;
@troyharris
troyharris / gist:6057431
Last active December 20, 2015 02:39
Convert iPad fontSize to iPhone and Vice-Versa
#
# As far as I can tell, iOS treats fontSize based on PPI. It's not completely perfect but here is a good ratio to use.
# Spoiler: It's 2.591
#
+(CGFloat)getFontSizeFromIPadFontSize:(CGFloat)fontSize {
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
fontSize = fontSize / 2.591;
}
return fontSize;
@troyharris
troyharris / gist:6152507
Last active December 20, 2015 15:09
Get a random letter in Objective-C
// Get a random capitol letter
[NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'A'];
//...or lowercase
[NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a'];
@troyharris
troyharris / gist:6152519
Last active December 20, 2015 15:09
Get a random FlatUIColor in Objective-C using Grouper/FlatUIKit
// Requires UIColor+FlatUI.h from Grouper/FlatUIKit
static inline CGFloat skRandf() {
return rand() / (CGFloat) RAND_MAX;
}
static inline CGFloat skRand(CGFloat low, CGFloat high) {
return skRandf() * (high - low) + low;
}
@troyharris
troyharris / gist:6257332
Created August 17, 2013 15:06
iOS 7 fix for invisible UITextView text
/* Sometimes in iOS 7, a UITextView will be invisible until the user starts editing it or
the keyboard appears. Adding this somewhere after setup will fix this issue (either viewDidLoad or viewDidLayoutSubviews)
*/
[self.textView.layoutManager ensureLayoutForTextContainer:self.textView.textContainer];
@troyharris
troyharris / gist:6430742
Last active December 22, 2015 06:28
A simple linked list object in Objective-C
@interface TRHLinkedNode : NSObject
@property (nonatomic, strong) NSString *dogName;
@property (nonatomic, string) TRHLinkedNode *nextNode;
- (void)appendItemToEnd(TRHLinkedNode *)item;
+ (void)searchAndDeleteFromLinkedList:(NSString *)dog afterItem:(TRHLinkedNode)headNode;
@end
@troyharris
troyharris / gist:6877536
Created October 8, 2013 00:33
A very hacky way to enable UITextView to scroll to the cursor when typing. I believe I got this from someone on SO but I can't find the original source anymore
- (void)textViewDidChange:(UITextView *)textView {
[self _showTextViewCaretPosition:textView];
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
[self _showTextViewCaretPosition:textView];
}
- (void)_showTextViewCaretPosition:(UITextView *)textView {
@troyharris
troyharris / gist:7e72f66a9b4242a3975fd36670c7126b
Created June 20, 2017 15:50
Count lines of code recursively
# For Django in the root folder
find . -name '*.py' | xargs wc -l
# For Angular, in the ROOT/src/app folder
find . -name '*.ts' | xargs wc -l