Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
* Support basic HTML in styled labels
Browse files Browse the repository at this point in the history
* Support inline images in styled labels
* Load URLs when links are touched in styled labels
* Improve performance of styled label layout

Signed-off-by: Joakim Bodin <joakim.bodin@gmail.com>
  • Loading branch information
joehewitt authored and Joakim Bodin committed Apr 1, 2009
1 parent c089315 commit 6fc1660
Show file tree
Hide file tree
Showing 21 changed files with 577 additions and 211 deletions.
10 changes: 10 additions & 0 deletions samples/TTCatalog/Classes/RootViewController.m
Expand Up @@ -111,4 +111,14 @@ - (void)willNavigateToObject:(id)object inView:(NSString*)viewType
viewController.title = field.text;
}

- (BOOL)shouldLoadExternalURL:(NSURL*)url {
NSString* message = [NSString stringWithFormat:@"You touched a link to %@", url];
UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:@"Link"
message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", @"")
otherButtonTitles:nil] autorelease];
[alertView show];

return NO;
}

@end
3 changes: 2 additions & 1 deletion samples/TTCatalog/Classes/StyledTextTableTestController.m
Expand Up @@ -26,6 +26,7 @@ - (void)loadView {
@"This is a whole bunch of text made from characters and followed by this url http://bit.ly/1234",
@"Here we have a url http://www.h0tlinkz.com followed by another http://www.internets.com",
@"http://www.cnn.com is a url and the words you are now reading are the text that follows",
@"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 All @@ -34,7 +35,7 @@ - (void)loadView {
TTListDataSource* dataSource = [[[TTListDataSource alloc] init] autorelease];
for (int i = 0; i < 50; ++i) {
NSString* string = [strings objectAtIndex:i % strings.count];
TTStyledText* text = [TTStyledText textFromURLString:string];
TTStyledText* text = [TTStyledText textWithURLs:string];

// Add a bold prefix to the text
NSString* title = [NSString stringWithFormat:@"Row %d: ", i+1];
Expand Down
28 changes: 20 additions & 8 deletions samples/TTCatalog/Classes/StyledTextTestController.m
Expand Up @@ -8,16 +8,28 @@ @implementation StyledTextTestController
- (void)loadView {
[super loadView];

NSString* kSampleText = @"This is a test of http://foo.com styled text. This test will \
be more interesting when I implement the HTML parser. See the 'Styled Labels in Table' test \
for another example of styled text. Gratuitous URL alert: http://www.foo.com";
NSString* kText = @"This is a test of styled labels. Styled labels support \
<b>bold text</b> and <i>italic text</i>. They also support \
<a href=\"http://www.google.com\">hyperlinks</a> and inline images \
<img src=\"bundle://smiley.png\"/>. You can also embed a URL inline and it will be turned into \
a link, like the following URL: http://www.foo.com";

TTStyledLabel* label = [[[TTStyledLabel alloc] initWithFrame:
TTStyledLabel* label1 = [[[TTStyledLabel alloc] initWithFrame:
CGRectInset(self.view.bounds, 10, 10)] autorelease];
label.font = [UIFont systemFontOfSize:18];
label.text = [TTStyledText textFromURLString:kSampleText];

[self.view addSubview:label];
label1.font = [UIFont systemFontOfSize:17];
label1.text = [TTStyledText textFromXHTML:kText];
[label1 sizeToFit];
[self.view addSubview:label1];

TTStyledLabel* label2 = [[[TTStyledLabel alloc] initWithFrame:
CGRectInset(self.view.bounds, 10, 10)] autorelease];
label2.font = [UIFont systemFontOfSize:12];
label2.text = [TTStyledText textFromXHTML:kText];
label2.textColor = [UIColor grayColor];
[label2 sizeToFit];
label2.top = label1.bottom + 20;
[self.view addSubview:label2];
}

@end

4 changes: 4 additions & 0 deletions samples/TTCatalog/TTCatalog.xcodeproj/project.pbxproj
Expand Up @@ -15,6 +15,7 @@
2899E5600DE3E45000AC0155 /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2899E55F0DE3E45000AC0155 /* RootViewController.xib */; };
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
28C286E10D94DF7D0034E888 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28C286E00D94DF7D0034E888 /* RootViewController.m */; };
BE3188A10F822E2C00E3067D /* smiley.png in Resources */ = {isa = PBXBuildFile; fileRef = BE3188A00F822E2C00E3067D /* smiley.png */; };
BE5F25920EBA5F0400FD59A6 /* PhotoTest2Controller.m in Sources */ = {isa = PBXBuildFile; fileRef = BE5F25910EBA5F0400FD59A6 /* PhotoTest2Controller.m */; };
BE69B7590F62874900C02928 /* TableTestController.m in Sources */ = {isa = PBXBuildFile; fileRef = BE69B7580F62874900C02928 /* TableTestController.m */; };
BE6E46EC0F4578BA001CE9B4 /* TabBarTestController.m in Sources */ = {isa = PBXBuildFile; fileRef = BE6E46EB0F4578BA001CE9B4 /* TabBarTestController.m */; };
Expand Down Expand Up @@ -70,6 +71,7 @@
28C286E00D94DF7D0034E888 /* RootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BE3188A00F822E2C00E3067D /* smiley.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = smiley.png; sourceTree = "<group>"; };
BE5F25900EBA5F0400FD59A6 /* PhotoTest2Controller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhotoTest2Controller.h; sourceTree = "<group>"; };
BE5F25910EBA5F0400FD59A6 /* PhotoTest2Controller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PhotoTest2Controller.m; sourceTree = "<group>"; };
BE69B7570F62874900C02928 /* TableTestController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableTestController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,6 +209,7 @@
BE80E39C0EAF103200743358 /* DefaultAlbum.png */,
BE6E4DBF0F46A352001CE9B4 /* tableIcon.png */,
BECB1CC10F46AE9600AE5B52 /* person.jpg */,
BE3188A00F822E2C00E3067D /* smiley.png */,
8D1107310486CEB800E47090 /* Info.plist */,
);
name = Resources;
Expand Down Expand Up @@ -304,6 +307,7 @@
BE6E4DC00F46A352001CE9B4 /* tableIcon.png in Resources */,
BECB1CC20F46AE9600AE5B52 /* person.jpg in Resources */,
BEDCFBB40F4FFF820060B7D1 /* Three20.bundle in Resources */,
BE3188A10F822E2C00E3067D /* smiley.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file added samples/TTCatalog/smiley.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/TTNavigationCenter.m
Expand Up @@ -440,7 +440,10 @@ - (TTViewController*)displayURL:(NSString*)url withState:(NSDictionary*)state
animated:(BOOL)animated {
NSURL* u = [NSURL URLWithString:url];
if ([_urlSchemes indexOfObject:u.scheme] == NSNotFound) {
[[UIApplication sharedApplication] openURL:u];
if (![_delegate respondsToSelector:@selector(shouldLoadExternalURL:)]
|| [_delegate shouldLoadExternalURL:u]) {
[[UIApplication sharedApplication] openURL:u];
}
} else if (_viewLoaders) {
id<TTPersistable> object = [self locateObject:u];
NSString* viewType = object && u.query ? u.query : u.host;
Expand Down
28 changes: 24 additions & 4 deletions src/TTStyledLabel.m
Expand Up @@ -77,9 +77,13 @@ - (void)layoutSubviews {
}

- (CGSize)sizeThatFits:(CGSize)size {
_text.width = size.width;
return CGSizeMake(_text.width, _text.height);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// UIResponder

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
Expand All @@ -97,8 +101,7 @@ - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (_highlightedNode) {
// XXXjoe Still deciding whether to do this, or use a delegate
// [[TTNavigationCenter defaultCenter] displayURL:_highlightedNode.url];
[[TTNavigationCenter defaultCenter] displayURL:_highlightedNode.url];

self.highlightedNode = nil;

Expand Down Expand Up @@ -164,22 +167,36 @@ - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if (self = [super initWithFrame:frame style:style]) {
_highlightedLabel = nil;
_highlightStartPoint = CGPointZero;
_highlightTimer = nil;
self.delaysContentTouches = NO;
}
return self;
}

- (void)dealloc {
[_highlightedLabel release];
[_highlightTimer invalidate];
[super dealloc];
}

- (void)delayedTouchesEnded:(NSTimer*)timer {
_highlightTimer = nil;

self.highlightedLabel = nil;

NSString* url = timer.userInfo;
[[TTNavigationCenter defaultCenter] displayURL:url];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
// UIResponder

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];

[_highlightTimer invalidate];
_highlightTimer = nil;

if (_highlightedLabel) {
UITouch* touch = [touches anyObject];
_highlightStartPoint = [touch locationInView:self];
Expand All @@ -204,8 +221,11 @@ - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (_highlightedLabel) {
NSString* url = _highlightedLabel.highlightedNode.url;
_highlightedLabel.highlightedNode = nil;
self.highlightedLabel = nil;

_highlightTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self
selector:@selector(delayedTouchesEnded:) userInfo:url repeats:NO];
} else {
[super touchesEnded:touches withEvent:event];
}
Expand Down

0 comments on commit 6fc1660

Please sign in to comment.