Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
zaneclaes / gist:6858191
Last active December 24, 2015 20:29
How to measure text on both iPhone and OSX (sizeWithFont for OSX)
// Part of a category on NSString...
- (CGSize)sizeWithFontName:(NSString*)fontName ofSize:(float)size constrainedTo:(CGSize)constraint {
#ifdef __CC_PLATFORM_MAC
NSFont *font = [NSFont fontWithName:fontName size:size];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:self];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:constraint];
;
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
@zaneclaes
zaneclaes / gist:6967461
Created October 13, 2013 21:10
Drawing a trapezoidal polygon in cocos2d
static const CGFloat kPerspectiveAngle = 3;//3 degree rotation
#define PERSPECTIVE_CONSTRICT_PX_FOR_HEIGHT(h) roundf(tanf(kPerspectiveAngle / 180.f * M_PI)*h)
- (void)drawTerrainPolygon {
static const int bottom = 23; // The padding at the bottom of the screen
int constrict = PERSPECTIVE_CONSTRICT_PX_FOR_HEIGHT(self.contentSize.height);//Determine how many pixels to contract by
CGPoint pts[] = { ccp(0,bottom),
ccp(constrict,self.contentSize.height),
@interface AMSprite ()
@property (nonatomic, strong) CCSprite *shadowSprite;
@end
@implementation AMSprite
- (void)addShadow {
if(self.shadowSprite) {
return;
@implementation AMLayer
- (void)visit {
CGRect mask = self.maskArea;
BOOL shouldMask = !CGSizeEqualToSize(mask.size, CGSizeZero);
if(shouldMask) {
CGFloat s = CC_CONTENT_SCALE_FACTOR();
CGPoint origin = CGPointMake(mask.origin.x*s, mask.origin.y*s);
CGSize size = CGSizeMake(mask.size.width*s, mask.size.height*s);
@zaneclaes
zaneclaes / gist:7400737
Created November 10, 2013 16:55
Detecting hit areas on a cocos2d sprite
@implementation MySprite
// The internal rect representing the current texture
- (CGRect)hitArea {
CGPoint bl = CGPointMake(MIN(_quad.tl.vertices.x, _quad.bl.vertices.x), MIN(_quad.bl.vertices.y, _quad.br.vertices.y));
CGPoint tr = CGPointMake(MAX(_quad.tr.vertices.x, _quad.br.vertices.x), MAX(_quad.tl.vertices.y, _quad.tr.vertices.y));
return CGRectMake(bl.x, bl.y, tr.x - bl.x, tr.y - bl.y);
}
// Works just like .boundingBox (eg, represents the hitArea in parent coordinates)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BBInputTableViewCell *cell = (BBInputTableViewCell *)[tableView dequeueCellOfClass:[BBInputTableViewCell class]];
cell.backgroundStyle = BBTableViewCellBackgroundStyleCard;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
BBInputTableViewCellAttributes *attributes = [[BBInputTableViewCellAttributes alloc] init];
switch (indexPath.row) {
case BBDebugToolVagrant: {
[attributes setSwitchPrompt:@"Vagrant?" isOn:NO handlerBlock:^(id sender, BBInputTableViewCellAttributes *attributes, id value, id arguments) {
describe "#social_connections" do
before do
# Social connections are cached within the model. To prevent cross-talk
# between the tests, make sure to clear out Rails' cache.
Rails.cache.clear
@target_user = FactoryGirl.create(:user)
@current_user = FactoryGirl.create(:user)
FactoryGirl.create(:relationship, :user => @current_user, :friend => @target_user)
describe "#social_connections" do
before :each do
@target_user = FactoryGirl.create(:user)
@target_user_listing = FactoryGirl.create(:listing, :user => @target_user)
@current_user = FactoryGirl.create(:user)
@relationship = FactoryGirl.create(:relationship, :user => @current_user, :friend => @target_user)
@params = {:id => @target_user.id, :api_version => 'v1'}
end
context 'when logged in as a befriended user' do
if(self.socialConnections.count == 0) {
return @"EMPTY STATE...";// TODO: ZC What does the empty state look like?
}
else if(self.socialConnections.count == 1) {
return connection.caption;
}
else if(self.socialConnections.count == 2) {
BBSocialConnection *otherConnection = self.socialConnections[1];
return [NSString stringWithFormat:BBLocalizedString(@"mobile.all.connections_single", @"%@ and %@ are connected to %@", @"Viewing a user profile: people are connected through facebook friends, etc."),connection.targetUser.shortName,otherConnection.targetUser.shortName,self.user.shortName];
}
connections_objects = connections.each {|sc| sc.populate(:context => SocialConnection::CONTEXT_USER)};
connections_json = connections_objects.map{|sc| sc.as_json };
connections_json = connections_json.each{|sc| sc['test'] = 'one' };
render :json => {:connections_count => connections.count, :connections => connections_json}