Skip to content

Instantly share code, notes, and snippets.

View chrishulbert's full-sized avatar

Chris Hulbert chrishulbert

View GitHub Profile
@chrishulbert
chrishulbert / ssdp.node.js
Created March 30, 2011 21:46
Service discovery using node.js and ssdp / universal plug n play
var dgram = require('dgram'); // dgram is UDP
// Listen for responses
function listen(port) {
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
});
@chrishulbert
chrishulbert / gist:902774
Created April 5, 2011 00:24
Python unix socket example
import socket
minissdpdSocket = '/var/run/minissdpd.sock' # The socket for talking to minissdpd
# Sends a message to the given socket
def sendMessage(message):
s = socket.socket(socket.AF_UNIX)
s.settimeout(1) # 1 second timeout, after all it should be instant because its local
s.connect(minissdpdSocket)
s.send(message)
@chrishulbert
chrishulbert / bcastpacket.m
Created April 6, 2011 01:23
Send a broadcast udp message using c / obj-c
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
- (void)sendBroadcastPacket {
// Open a socket
int sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sd<=0) {
NSLog(@"Error: Could not open socket");
@chrishulbert
chrishulbert / radial.m
Created April 11, 2011 23:08
Rendering a radial gradient on the iphone
- (UIImage *)radialGradientImage:(CGSize)size start:(float)start end:(float)end centre:(CGPoint)centre radius:(float)radius {
// Render a radial background
// http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html
// Initialise
UIGraphicsBeginImageContextWithOptions(size, YES, 1);
// Create the gradient's colours
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
@chrishulbert
chrishulbert / buildquerystring.m
Created April 13, 2011 02:17
Build a url query string in obj-c from a dictionary of params like jquery does
+(NSString*)urlEscape:(NSString *)unencodedString {
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
kCFStringEncodingUTF8);
return [s autorelease]; // Due to the 'create rule' we own the above and must autorelease it
}
// Put a query string onto the end of a url
@chrishulbert
chrishulbert / SizableImageCell.m
Created April 21, 2011 06:18
Make a UITableViewCell with a resized image
@interface SizableImageCell : UITableViewCell {}
@end
@implementation SizableImageCell
- (void)layoutSubviews {
[super layoutSubviews];
float desiredWidth = 80;
float w=self.imageView.frame.size.width;
if (w>desiredWidth) {
float widthSub = w - desiredWidth;
@chrishulbert
chrishulbert / multi line label.m
Created April 28, 2011 04:59
How to make a multiline label with dynamic text on the iphone and get the correct height
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 9999)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = @"Some \n dynamic \n multiline \n text";
[myLabel sizeToFit]; // This shrinks the 9999 down to the size of the text
NSLog(@"Actual height is: %f", myLabel.frame.size.height); // Use this for spacing any further elements
[self.view addSubview:title]; // Or add it to a scroll view, or whatever...
[myLabel release];
@chrishulbert
chrishulbert / centered_header.m
Created May 6, 2011 03:58
Centered header on a uitableview
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
lbl.text = @"My Centered Header";
lbl.textColor = [UIColor whiteColor];
lbl.shadowColor = [UIColor grayColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
lbl.alpha = 0.9;
@chrishulbert
chrishulbert / swiperama.m
Created May 6, 2011 04:36
How to make an iphone view controller detect left or right swipes
- (void)swipeLeft {
NSLog(@"Left!");
}
- (void)swipeRight {
NSLog(@"Right!");
}
- (void)viewDidLoad
{
[super viewDidLoad];
@chrishulbert
chrishulbert / gcd.m
Created May 10, 2011 06:16
Tricks with grand central dispatch
// Using grand central dispatch (GCD) so we don't block the GUI
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// Background, long stuff runs here, but doesn't affect the GUI
dispatch_async(dispatch_get_main_queue(), ^{
// The GUI thread stuff goes here
[self.tableView reloadData]; // example