-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternative implementation using UIKit (TextBoxView)
- Loading branch information
1 parent
e7d71bb
commit d0d9a09
Showing
9 changed files
with
266 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// TextBox.h | ||
// TextBoxLayerSample | ||
// | ||
// Created by Fabio Rodella on 04/10/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#define TEXT_SPEED 60 | ||
#define TEXT_FONT_FILE @"arial16.fnt" | ||
|
||
@protocol TextBox <NSObject> | ||
|
||
- (id) initWithColor:(UIColor *)color | ||
width:(CGFloat)w | ||
height:(CGFloat)h | ||
padding:(CGFloat)padding | ||
text:(NSString *)txt; | ||
|
||
- (void)update:(float)dt; | ||
|
||
@end | ||
|
||
@protocol TextBoxDelegate <NSObject> | ||
|
||
- (void)textBox:(id<TextBox>)tbox didFinishAllTextWithPageCount:(int)pc; | ||
|
||
@optional | ||
- (void)textBox:(id<TextBox>)tbox didMoveToPage:(int)p; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// TextBoxView.h | ||
// TextBoxLayerSample | ||
// | ||
// Created by Fabio Rodella on 04/10/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "TextBox.h" | ||
|
||
@interface TextBoxView : UIView <TextBox> { | ||
|
||
CGFloat width; | ||
CGFloat height; | ||
|
||
UILabel *label; | ||
NSString *text; | ||
float progress; | ||
|
||
int currentPage; | ||
|
||
NSMutableArray *pages; | ||
|
||
id<TextBoxDelegate> delegate; | ||
} | ||
|
||
@property (readwrite,assign) id<TextBoxDelegate> delegate; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// TextBoxView.m | ||
// TextBoxLayerSample | ||
// | ||
// Created by Fabio Rodella on 04/10/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "TextBoxView.h" | ||
|
||
|
||
@implementation TextBoxView | ||
|
||
@synthesize delegate; | ||
|
||
- (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding:(CGFloat)padding text:(NSString *)txt { | ||
|
||
if ((self = [super initWithFrame:CGRectMake(0, 0, w + (padding * 2), h + (padding * 2))])) { | ||
self.backgroundColor = color; | ||
|
||
width = w; | ||
height = h; | ||
|
||
currentPage = 0; | ||
text = [txt retain]; | ||
|
||
label = [[UILabel alloc] initWithFrame:CGRectMake(padding, padding, w, h)]; | ||
label.text = @""; | ||
label.backgroundColor = [UIColor clearColor]; | ||
label.numberOfLines = 0; | ||
[self addSubview:label]; | ||
[label release]; | ||
|
||
NSArray *words = [txt componentsSeparatedByString:@" "]; | ||
|
||
NSString *curText = [words objectAtIndex:0]; | ||
int i = 1; | ||
|
||
pages = [[NSMutableArray alloc] init]; | ||
|
||
while (i < [words count]) { | ||
|
||
NSString *nextText = [curText stringByAppendingFormat:@" %@",[words objectAtIndex:i]]; | ||
CGSize size = [nextText sizeWithFont:label.font constrainedToSize:CGSizeMake(w, 99999) lineBreakMode:UILineBreakModeWordWrap]; | ||
|
||
if (size.height > h) { | ||
[pages addObject:curText]; | ||
curText = [words objectAtIndex:i]; | ||
} else { | ||
curText = nextText; | ||
} | ||
i++; | ||
} | ||
[pages addObject:curText]; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
- (void)dealloc | ||
{ | ||
[pages release]; | ||
[text release]; | ||
[super dealloc]; | ||
} | ||
|
||
- (void)update:(float)dt { | ||
|
||
NSString *page = [pages objectAtIndex:currentPage]; | ||
|
||
progress += (dt * TEXT_SPEED); | ||
|
||
int visible = progress; | ||
if (visible > [page length]) { | ||
visible = [page length]; | ||
} | ||
|
||
NSString *newTxt = [page substringToIndex:visible]; | ||
|
||
label.text = newTxt; | ||
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, width, height); | ||
[label sizeToFit]; | ||
} | ||
|
||
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | ||
|
||
NSString *page = [pages objectAtIndex:currentPage]; | ||
|
||
int visible = progress; | ||
if (visible >= [page length]) { | ||
|
||
if (currentPage == [pages count] - 1) { | ||
if ([delegate respondsToSelector:@selector(textBox:didFinishAllTextWithPageCount:)]) { | ||
[delegate textBox:(id<TextBox>) self didFinishAllTextWithPageCount:[pages count]]; | ||
} | ||
} else { | ||
currentPage++; | ||
progress = 0; | ||
|
||
if ([delegate respondsToSelector:@selector(textBox:didMoveToPage:)]) { | ||
[delegate textBox:(id<TextBox>) self didMoveToPage:currentPage]; | ||
} | ||
} | ||
|
||
} else { | ||
progress = [page length]; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
CCLayer subclass designed to handle RPG-like text boxes. More information: http://www.crocodella.com.br/2011/01/rpg-like-text-box-with-cocos2d/ | ||
CCLayer subclass designed to handle RPG-like text boxes. Also included is an alternative implementation using UIKit instead. More information: http://www.crocodella.com.br/2011/01/rpg-like-text-box-with-cocos2d/ |
Oops, something went wrong.