Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Created January 16, 2011 23:52
Show Gist options
  • Save jonsterling/782276 to your computer and use it in GitHub Desktop.
Save jonsterling/782276 to your computer and use it in GitHub Desktop.
//
// BubbleView.h
//
// Created by Josh Kendall on 12/28/10.
// Copyright 2010 JoshuaKendall.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface BubbleView : NSObject {
int maxWidth, x, s, i, count, totalWidth;
CGPoint p;
}
@property (nonatomic, retain) UIView *bubble;
@property (nonatomic, retain) CAGradientLayer *gradient;
- (UIView *)createTokensFrom:(NSString *)label withPosition:(CGPoint)point withMaxWidth:(int)width;
- (UIView *)createTokensFrom:(NSString *)label withPosition:(CGPoint)point;
- (void)createTokens:(NSString *)string;
- (UIView *)createBubbleWithString:(NSString *)token;
@end
//
// BubbleView.m
//
// Created by Josh Kendall on 12/28/10.
// Copyright 2010 JoshuaKendall.com. All rights reserved.
//
#import "BubbleView.h"
@interface BubbleView ()
@property (nonatomic,retain) NSArray *tokens;
@end
@implementation BubbleView
@synthesize bubble;
@synthesize gradient;
@synthesize tokens;
- (id)init {
self = [super init];
if (self != nil) {
bubble = [[UIView alloc] init];
maxWidth = 0;
totalWidth = 0;
x = 0;
s = 0;
i = 0;
count = 0;
}
return self;
}
- (UIView *)createTokensFrom:(NSString *)label withPosition:(CGPoint)point withMaxWidth:(int)width; {
p = point;
maxWidth = width;
[self createTokens:label];
for(NSString *token in tokens){
if(totalWidth < maxWidth){
UIView *b = [self createBubbleWithString:token];
[bubble addSubview:b];
}
}
if(count > 0){
CGSize sizeOfLabel = [[NSString stringWithFormat:@"& %d More", count] sizeWithFont:[UIFont systemFontOfSize:11]];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((p.x + s), point.y, (sizeOfLabel.width + 16), (sizeOfLabel.height + 5))];
[title setText:[NSString stringWithFormat:@"& %d More", count]];
[title setFont:[UIFont systemFontOfSize:11]];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [UIColor blackColor];
[bubble addSubview:title];
[title release];
title = nil;
}
return bubble;
}
- (UIView *)createTokensFrom:(NSString *)label withPosition:(CGPoint)point; {
p = point;
[self createTokens:label];
for(NSString *token in tokens){
[bubble addSubview:[self createBubbleWithString:token]];
}
return bubble;
}
- (void)createTokens:(NSString *)string; {
tokens = [string componentsSeparatedByString: @","];
count = [tokens count];
}
- (UIView *)createBubbleWithString:(NSString *)token; {
CGSize sizeOfLabel = [token sizeWithFont:[UIFont systemFontOfSize:11]];
p.x = (p.x + s);
s = (sizeOfLabel.width + 20);
totalWidth = totalWidth + s;
CGRect rect = CGRectMake(p.x, p.y, (sizeOfLabel.width + 16), (sizeOfLabel.height + 5));
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (sizeOfLabel.width + 16), (sizeOfLabel.height + 5))];
UIView *container = [[[UIView alloc] initWithFrame:rect] autorelease];
[title setFont:[UIFont systemFontOfSize:11]];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [UIColor blackColor];
title.layer.cornerRadius = 10;
title.layer.borderColor = [UIColor colorWithRed:0.678 green:0.733 blue:0.847 alpha:1].CGColor;
title.layer.borderWidth = .5;
title.backgroundColor = [UIColor clearColor];
[title setText:token];
gradient = [CAGradientLayer layer];
gradient.frame = container.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.847 green:0.898 blue:1 alpha:1] CGColor], (id)[[UIColor colorWithRed:0.741 green:0.796 blue:1 alpha:1] CGColor], nil];
gradient.cornerRadius = 10;
[container.layer insertSublayer:gradient atIndex:0];
[container addSubview:title];
[title release];
title = nil;
i = i + 1;
count--;
return container;
}
- (void)dealloc {
[gradient release];
[bubble release];
[tokens release];
[super dealloc];
}
@end
# Show all bubbles.
BubbleView *bv = [[BubbleView alloc] init];
UIView *tags = [bv createTokensFrom:@"PHP,MySQL,iOS" withPosition:CGPointMake(90, 12)];
[self.view addSubview:bubble];
# Show only the bubbles that fit in the specified width.
BubbleView *bv = [[BubbleView alloc] init];
UIView *tags = [bv createTokensFrom:@"PHP,MySQL,iOS,C++,AJAX,ASP.NET,SQLite,Ruby" withPosition:CGPointMake(90, 12) withMaxWidth:100];
[self.view addSubview:bubble];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment