Skip to content

Instantly share code, notes, and snippets.

View wess's full-sized avatar
💭
When am I not writing code?

Wess Cope wess

💭
When am I not writing code?
View GitHub Profile
@wess
wess / WCAttributedString.h
Created March 29, 2012 21:18
Content View using core text with 1 embedded image
//
// WCAttributedString.h
// iOS
//
// Created by Wess Cope on 3/19/12.
//
// Special Thanks to Erica Sadun's Cookbook For the following, updated just a bit.
//
#import <Foundation/Foundation.h>
@wess
wess / gist:2413505
Created April 18, 2012 13:18
CORETEXT Layout
#define ARTICLE_IMAGE_BORDER_SIZE 4.0f
@implementation LOArticleContentView
@synthesize text = _text;
@synthesize image = _image;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
@wess
wess / gist:2644790
Created May 9, 2012 14:17
Hacked Image Preloader for gerburms
(function(){
var imagePreload = function(imageUrlList)
{
for(var i = 0; i < imageUrlList.length; i++)
{
var image = new Image();
image.src = imageUrlList[i];
}
}
@wess
wess / gist:2772021
Created May 22, 2012 22:21
Example Syntax for XML parser
[WCXMLParser parseXMLString:operation.responseString withOptions:0 success:^(WCXMLParser *parser, id parsedObject) {
NSLog(@"PARSED: %@", parsedObject);
} failure:^(WCXMLParser *parser, NSError *error) {
NSLog(@"ERROR: %@", [error description]);
}];
@wess
wess / gist:3136429
Created July 18, 2012 14:14
Get a clipping rect for CoreText
static CGRect clipRectToPath(CGRect rect, CGPathRef path)
{
size_t width = floorf(rect.size.width);
size_t height = floorf(rect.size.height);
uint8_t *points = calloc(width * height, sizeof(*points));
CGContextRef bitmapContext = CGBitmapContextCreate(points, width, height, sizeof(*points) * 8, width, NULL, kCGImageAlphaOnly);
BOOL atStart = NO;
NSRange range = NSMakeRange(0, 0);
NSUInteger x = 0;
@wess
wess / gist:3170667
Created July 24, 2012 15:29
CoreText content Height
- (CGSize)sizeForAttributedStringWithWidth:(CGFloat)width
{
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
CFIndex offset = 0, length;
CGFloat y = 0;
do {
length = CTTypesetterSuggestLineBreak(typesetter, offset, width);
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(offset, length));
@wess
wess / gist:3170684
Created July 24, 2012 15:31
another CT size try
- (CGFloat)getContentHeight
{
CTFramesetterRef tmpFrameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_body);
int textLength = [_body length];
CFRange range;
CGFloat maxWidth = self.width;
CGFloat maxHeight = CGFLOAT_MAX;
CGSize constraint = CGSizeMake(maxWidth, maxHeight);
CGSize contentSize = CTFramesetterSuggestFrameSizeWithConstraints(tmpFrameSetter, CFRangeMake(0, textLength), nil, constraint, &range);
@wess
wess / gist:3278911
Created August 6, 2012 22:12
Tapping in fucking CoreText
// A small chunk of it.
- (void)tapGestureAction:(UITapGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:self];
_linkLocation = location;
location.y += (ARTICLE_BODY_FONT_SIZE / lineHeightFactor);
CFArrayRef lines = CTFrameGetLines(_frame);
@wess
wess / gist:3353556
Created August 14, 2012 22:28
Convert command line arguments in Objective-c to an NSDictionary
// Supports --foo=bar and -f bar
static NSDictionary *arguments()
{
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
if(arguments.count < 2)
return nil;
NSMutableDictionary *argsDict = [[NSMutableDictionary alloc] init];
@wess
wess / gist:3360974
Created August 15, 2012 15:19
write option
[PRouter addOption:@[@"n", @"new"] callback:^(NSString *value) {
NSDictionary *manifest = @{
@"name" : @"",
@"description" : @"",
@"version" : @"",
@"author" : @"",
@"url" : @"",
@"keywords" : @[]
};