Skip to content

Commit

Permalink
* Speech bubble shape
Browse files Browse the repository at this point in the history
* Put padding back on styled text table cells
* Localize a few strings that I had missed
* Fix crash in documents and bundle urls
  • Loading branch information
joehewitt committed Apr 28, 2009
1 parent 3d17d04 commit e7a0d08
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 55 deletions.
17 changes: 16 additions & 1 deletion samples/TTCatalog/Classes/StyleTestController.m
Expand Up @@ -36,7 +36,21 @@ - (void)loadView {
[TTRoundedRectangleShape shapeWithTopLeft:0 topRight:0 bottomRight:10 bottomLeft:10] next:
[TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
[TTSolidBorderStyle styleWithColor:black width:1 next:nil]]],


// SpeechBubble
[TTShapeStyle styleWithShape:[TTSpeechBubbleShape shapeWithRadius:5 pointLocation:60
pointAngle:90
pointSize:CGSizeMake(20,10)] next:
[TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
[TTSolidBorderStyle styleWithColor:black width:1 next:nil]]],

// SpeechBubble
[TTShapeStyle styleWithShape:[TTSpeechBubbleShape shapeWithRadius:5 pointLocation:290
pointAngle:270
pointSize:CGSizeMake(20,10)] next:
[TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
[TTSolidBorderStyle styleWithColor:black width:1 next:nil]]],

// Drop shadow
[TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:10] next:
[TTShadowStyle styleWithColor:RGBACOLOR(0,0,0,0.5) blur:5 offset:CGSizeMake(2, 2) next:
Expand Down Expand Up @@ -98,6 +112,7 @@ - (void)loadView {
[TTMaskStyle styleWithMask:TTIMAGE(@"bundle://mask.png") next:
[TTLinearGradientFillStyle styleWithColor1:RGBCOLOR(0, 180, 231)
color2:RGBCOLOR(0, 0, 255) next:nil]],

nil];

CGFloat padding = 10;
Expand Down
43 changes: 8 additions & 35 deletions samples/TTCatalog/Classes/StyledTextTableTestController.m
@@ -1,34 +1,7 @@
#import "StyledTextTableTestController.h"

@interface TextTableTestStyleSheet : TTDefaultStyleSheet
@end

@implementation TextTableTestStyleSheet

- (TTStyle*)block {
return [TTBoxStyle styleWithMargin:UIEdgeInsetsMake(5,10,5,10) next:nil];
}

@end

@implementation StyledTextTableTestController


///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)init {
if (self = [super init]) {
[TTStyleSheet setGlobalStyleSheet:[[[TextTableTestStyleSheet alloc] init] autorelease]];
}
return self;
}

- (void)dealloc {
[TTStyleSheet setGlobalStyleSheet:nil];
[super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// UIViewController

Expand All @@ -50,14 +23,14 @@ - (void)loadView {

- (id<TTTableViewDataSource>)createDataSource {
NSArray* strings = [NSArray arrayWithObjects:
[TTStyledText textFromXHTML:@"<p class=\"block\">This is a whole bunch of text made from \
characters and followed by this url http://bit.ly/1234</p>"],
[TTStyledText textFromXHTML:@"<p class=\"block\">Here we have a url http://www.h0tlinkz.com \
followed by another http://www.internets.com</p>"],
[TTStyledText textFromXHTML:@"<p class=\"block\">http://www.cnn.com is a url and the words you \
are now reading are the text that follows</p>"],
[TTStyledText textFromXHTML:@"<p class=\"block\">Here is text that has absolutely no styles. \
Move along now. Nothing to see here. Goodbye now.</p>"],
[TTStyledText textFromXHTML:@"This is a whole bunch of text made from \
characters and followed by this url http://bit.ly/1234"],
[TTStyledText textFromXHTML:@"Here we have a url http://www.h0tlinkz.com \
followed by another http://www.internets.com"],
[TTStyledText textFromXHTML:@"http://www.cnn.com is a url and the words you \
are now reading are the text that follows"],
[TTStyledText textFromXHTML:@"Here is text that has absolutely no styles. \
Move along now. Nothing to see here. Goodbye now."],
// @"Let's test out some line breaks.\n\nOh yeah.",
// @"This is a message with a long url in it http://www.foo.com/abra/cadabra/abrabra/dabarababa",
nil];
Expand Down
12 changes: 10 additions & 2 deletions src/TTGlobal.m
Expand Up @@ -104,11 +104,19 @@ void TTNetworkRequestStopped() {
}

BOOL TTIsBundleURL(NSString* url) {
return [url rangeOfString:@"bundle://" options:0 range:NSMakeRange(0,9)].location == 0;
if (url.length >= 9) {
return [url rangeOfString:@"bundle://" options:0 range:NSMakeRange(0,9)].location == 0;
} else {
return NO;
}
}

BOOL TTIsDocumentsURL(NSString* url) {
return [url rangeOfString:@"documents://" options:0 range:NSMakeRange(0,12)].location == 0;
if (url.length >= 12) {
return [url rangeOfString:@"documents://" options:0 range:NSMakeRange(0,12)].location == 0;
} else {
return NO;
}
}

NSString* TTPathForBundleResource(NSString* relativePath) {
Expand Down
224 changes: 224 additions & 0 deletions src/TTShape.m
Expand Up @@ -7,6 +7,7 @@

static const CGFloat kArrowPointWidth = 2.8;
static const CGFloat kArrowRadius = 2;
static CGFloat kInsetWidth = 5;

#define RD(_RADIUS) (_RADIUS == TT_ROUNDED ? round(fh/2) : _RADIUS)

Expand Down Expand Up @@ -503,3 +504,226 @@ - (UIEdgeInsets)insetsForSize:(CGSize)size {
}

@end

///////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTSpeechBubbleShape

@synthesize radius = _radius, pointLocation = _pointLocation, pointAngle = _pointAngle,
pointSize = _pointSize;

///////////////////////////////////////////////////////////////////////////////////////////////////
// class public

+ (TTSpeechBubbleShape*)shapeWithRadius:(CGFloat)radius pointLocation:(CGFloat)pointLocation
pointAngle:(CGFloat)pointAngle pointSize:(CGSize)pointSize {
TTSpeechBubbleShape* shape = [[[TTSpeechBubbleShape alloc] init] autorelease];
shape.radius = radius;
shape.pointLocation = pointLocation;
shape.pointAngle = pointAngle;
shape.pointSize = pointSize;
return shape;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// private

- (CGRect)subtractPointFromRect:(CGRect)rect {
CGFloat x = 0;
CGFloat y = 0;
CGFloat w = rect.size.width;
CGFloat h = rect.size.height;

if ((_pointLocation >= 0 && _pointLocation < 45)
|| (_pointLocation >= 315 && _pointLocation < 360)) {
if ((_pointAngle >= 270 && _pointAngle < 360) || (_pointAngle >= 0 && _pointAngle < 90)) {
x += _pointSize.width;
w -= _pointSize.width;
}
} else if (_pointLocation >= 45 && _pointLocation < 135) {
if (_pointAngle >= 0 && _pointAngle < 180) {
y += _pointSize.height;
h -= _pointSize.height;
}
} else if (_pointLocation >= 135 && _pointLocation < 225) {
if (_pointAngle >= 90 && _pointAngle < 270) {
w -= _pointSize.width;
}
} else if (_pointLocation >= 225 && _pointLocation <= 315) {
if (_pointAngle >= 180 && _pointAngle < 360) {
h -= _pointSize.height;
}
}

return CGRectMake(x, y, w, h);
}

- (void)addTopEdge:(CGSize)size lightSource:(NSInteger)lightSource toPath:(CGMutablePathRef)path
reset:(BOOL)reset {
CGFloat fw = size.width;
CGFloat fh = size.height;
CGFloat ph = _pointSize.height;
CGFloat pointX = 0;

if (lightSource >= 0 && lightSource <= 90) {
if (reset) {
CGPathMoveToPoint(path, nil, RD(_radius), 0);
}
} else {
if (reset) {
CGPathMoveToPoint(path, nil, 0, RD(_radius));
}
CGPathAddArcToPoint(path, nil, 0, 0, RD(_radius), 0, RD(_radius));
}

if (_pointLocation >= 45 && _pointLocation <= 135) {
ph = _pointAngle >= 0 && _pointAngle < 180 ? _pointSize.height : -_pointSize.height;
pointX = ((_pointLocation-45)/90) * fw;

CGPathAddLineToPoint(path, nil, pointX-floor(_pointSize.width/2), 0);
CGPathAddLineToPoint(path, nil, pointX, -ph);
CGPathAddLineToPoint(path, nil, pointX+floor(_pointSize.width/2), 0);
}

CGPathAddArcToPoint(path, nil, fw, 0, fw, RD(_radius), RD(_radius));
}

- (void)addRightEdge:(CGSize)size lightSource:(NSInteger)lightSource toPath:(CGMutablePathRef)path
reset:(BOOL)reset {
CGFloat fw = size.width;
CGFloat fh = size.height;

if (reset) {
CGPathMoveToPoint(path, nil, fw, RD(_radius));
}

CGPathAddArcToPoint(path, nil, fw, fh, fw-RD(_radius), fh, RD(_radius));
}

- (void)addBottomEdge:(CGSize)size lightSource:(NSInteger)lightSource toPath:(CGMutablePathRef)path
reset:(BOOL)reset {
CGFloat fw = size.width;
CGFloat fh = size.height;
CGFloat ph = _pointSize.height;
CGFloat pointX = 0;

if (reset) {
CGPathMoveToPoint(path, nil, fw-RD(_radius), fh);
}

if (_pointLocation >= 225 && _pointLocation <= 315) {
if (_pointAngle >= 0 && _pointAngle < 180) {
ph = _pointSize.height;
} else {
ph = -_pointSize.height;
}

pointX = fw - (((_pointLocation-225)/90) * fw);
CGPathAddArcToPoint(path, nil, fw, fh, floor(fw/2), fh, RD(_radius));
CGPathAddLineToPoint(path, nil, pointX+floor(_pointSize.width/2), fh);
CGPathAddLineToPoint(path, nil, pointX, fh-ph);
CGPathAddLineToPoint(path, nil, pointX-floor(_pointSize.width/2), fh);
CGPathAddLineToPoint(path, nil, RD(_radius), fh);
}

CGPathAddArcToPoint(path, nil, 0, fh, 0, fh-RD(_radius), RD(_radius));
}

- (void)addLeftEdge:(CGSize)size lightSource:(NSInteger)lightSource toPath:(CGMutablePathRef)path
reset:(BOOL)reset {
CGFloat fh = size.height;

if (reset) {
CGPathMoveToPoint(path, nil, 0, fh-RD(_radius));
}

if (lightSource >= 0 && lightSource <= 90) {
CGPathAddArcToPoint(path, nil, 0, 0, RD(_radius), 0, RD(_radius));
} else {
CGPathAddLineToPoint(path, nil, 0, RD(_radius));
}
}

- (void)addToPath:(CGSize)size path:(CGMutablePathRef)path {
[self addTopEdge:size lightSource:0 toPath:path reset:YES];
[self addRightEdge:size lightSource:0 toPath:path reset:NO];
[self addBottomEdge:size lightSource:0 toPath:path reset:NO];
[self addLeftEdge:size lightSource:0 toPath:path reset:NO];
}

- (void)drawPath:(CGMutablePathRef)path inRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
CGContextAddPath(context, path);
CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (void)addToPath:(CGRect)rect {
[self openPath:rect];

CGMutablePathRef path = CGPathCreateMutable();
rect = [self subtractPointFromRect:rect];
[self addToPath:rect.size path:path];
CGPathCloseSubpath(path);
[self drawPath:path inRect:rect];
CGPathRelease(path);

[self closePath:rect];
}

- (void)addInverseToPath:(CGRect)rect {
[self openPath:rect];

CGMutablePathRef path = CGPathCreateMutable();
rect = [self subtractPointFromRect:rect];
CGRect shadowRect = CGRectMake(-kInsetWidth, -kInsetWidth,
rect.size.width+kInsetWidth*2, rect.size.height+kInsetWidth*2);
CGPathAddRect(path, nil, shadowRect);
[self addToPath:rect.size path:path];
CGPathCloseSubpath(path);
[self drawPath:path inRect:rect];
CGPathRelease(path);

[self closePath:rect];
}

- (void)addTopEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
rect = [self subtractPointFromRect:rect];

CGMutablePathRef path = CGPathCreateMutable();
[self addTopEdge:rect.size lightSource:lightSource toPath:path reset:YES];
[self drawPath:path inRect:rect];
CGPathRelease(path);
}

- (void)addRightEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
rect = [self subtractPointFromRect:rect];

CGMutablePathRef path = CGPathCreateMutable();
[self addRightEdge:rect.size lightSource:lightSource toPath:path reset:YES];
[self drawPath:path inRect:rect];
CGPathRelease(path);
}

- (void)addBottomEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
rect = [self subtractPointFromRect:rect];

CGMutablePathRef path = CGPathCreateMutable();
[self addBottomEdge:rect.size lightSource:lightSource toPath:path reset:YES];
[self drawPath:path inRect:rect];
CGPathRelease(path);
}

- (void)addLeftEdgeToPath:(CGRect)rect lightSource:(NSInteger)lightSource {
rect = [self subtractPointFromRect:rect];

CGMutablePathRef path = CGPathCreateMutable();
[self addLeftEdge:rect.size lightSource:lightSource toPath:path reset:YES];
[self drawPath:path inRect:rect];
CGPathRelease(path);
}

@end
4 changes: 3 additions & 1 deletion src/TTTableField.m
Expand Up @@ -364,7 +364,7 @@ - (void)dealloc {

@implementation TTStyledTextTableField

@synthesize styledText = _styledText;
@synthesize styledText = _styledText, margin = _margin, padding = _padding;

- (id)initWithStyledText:(TTStyledText*)styledText {
if (self = [self init]) {
Expand All @@ -383,6 +383,8 @@ - (id)initWithStyledText:(TTStyledText*)styledText url:(NSString*)url {
- (id)init {
if (self = [super init]) {
_styledText = nil;
_margin = UIEdgeInsetsZero;
_padding = UIEdgeInsetsMake(10, 10, 10, 10);
}
return self;
}
Expand Down

0 comments on commit e7a0d08

Please sign in to comment.