Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Kompanez committed Jun 12, 2011
1 parent 7fa2e61 commit 4e0e18e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion RTLabelProject/Classes/RTLabel.h
Expand Up @@ -66,7 +66,7 @@ typedef enum
RTTextAlignment _textAlignment;
RTTextLineBreakMode _lineBreakMode;
NSString *_plainText;
NSMutableArray *_textComponent;
NSMutableArray *_textComponents;
CGSize _optimumSize;
CGFloat _lineSpacing;
int currentSelectedButtonComponentIndex;
Expand Down
36 changes: 20 additions & 16 deletions RTLabelProject/Classes/RTLabel.m
Expand Up @@ -39,6 +39,7 @@

@interface RTLabelButton : UIButton
{
@private
int componentIndex;
NSURL *url;
}
Expand All @@ -63,6 +64,7 @@ - (void)dealloc

@interface RTLabelComponent : NSObject
{
@private
NSString *text;
NSString *tagLabel;
NSMutableDictionary *attributes;
Expand All @@ -77,9 +79,9 @@ @interface RTLabelComponent : NSObject
@property (nonatomic, assign) int position;

- (id)initWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes;
+ (id)componentWithString:(NSString*)_text tag:(NSString*)_tagLabel attributes:(NSMutableDictionary*)theAttributes;
+ (id)componentWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes;
- (id)initWithTag:(NSString*)aTagLabel position:(int)_position attributes:(NSMutableDictionary*)_attributes;
+ (id)componentWithTag:(NSString*)_tagLabel position:(int)_position attributes:(NSMutableDictionary*)_attributes;
+ (id)componentWithTag:(NSString*)aTagLabel position:(int)aPosition attributes:(NSMutableDictionary*)theAttributes;

@end

Expand Down Expand Up @@ -107,13 +109,13 @@ + (id)componentWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(
return [[[self alloc] initWithString:aText tag:aTagLabel attributes:theAttributes] autorelease];
}

- (id)initWithTag:(NSString*)aTagLabel position:(int)_position attributes:(NSMutableDictionary*)_attributes
- (id)initWithTag:(NSString*)aTagLabel position:(int)aPosition attributes:(NSMutableDictionary*)theAttributes
{
self = [super init];
if (self) {
self.tagLabel = aTagLabel;
self.position = _position;
self.attributes = _attributes;
tagLabel = [aTagLabel copy];
position = aPosition;
attributes = [theAttributes retain];
}
return self;
}
Expand Down Expand Up @@ -148,7 +150,7 @@ @interface RTLabel()

@property (nonatomic, retain) NSString *_text;
@property (nonatomic, retain) NSString *_plainText;
@property (nonatomic, retain) NSMutableArray *_textComponent;
@property (nonatomic, retain) NSMutableArray *_textComponents;
@property (nonatomic, assign) CGSize _optimumSize;

- (CGFloat)frameHeight:(CTFrameRef)frame;
Expand All @@ -161,6 +163,7 @@ - (void)extractTextStyle:(NSString*)text;

#pragma mark -
#pragma mark styling

- (void)applyItalicStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyBoldStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyColor:(NSString*)value toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
Expand All @@ -172,10 +175,11 @@ - (void)applyParagraphStyleToText:(CFMutableAttributedStringRef)text attributes:
@end

@implementation RTLabel

@synthesize _text;
@synthesize font;
@synthesize textColor;
@synthesize _plainText, _textComponent;
@synthesize _plainText, _textComponents;
@synthesize _optimumSize;
@synthesize linkAttributes;
@synthesize selectedLinkAttributes;
Expand Down Expand Up @@ -263,9 +267,9 @@ - (void)render

NSMutableArray *links = [NSMutableArray array];

for (RTLabelComponent *component in self._textComponent)
for (RTLabelComponent *component in self._textComponents)
{
int index = [self._textComponent indexOfObject:component];
int index = [self._textComponents indexOfObject:component];
component.componentIndex = index;

if ([component.tagLabel isEqualToString:@"i"])
Expand Down Expand Up @@ -784,7 +788,7 @@ - (void)dealloc
delegate = nil;
//CFRelease(frame);
//CFRelease(framesetter);
[_textComponent release];
[_textComponents release];
[_plainText release];
[textColor release];
[font release];
Expand Down Expand Up @@ -847,7 +851,7 @@ - (void)extractTextStyle:(NSString*)data
{
//NSLog(@"%@", data);

NSScanner *scanner;
NSScanner *scanner = nil;
NSString *text = nil;
NSString *tag = nil;

Expand All @@ -856,7 +860,7 @@ - (void)extractTextStyle:(NSString*)data
int last_position = 0;
scanner = [NSScanner scannerWithString:data];
while (![scanner isAtEnd])
{
{
[scanner scanUpToString:@"<" intoString:NULL];
[scanner scanUpToString:@">" intoString:&text];

Expand Down Expand Up @@ -926,15 +930,15 @@ - (void)extractTextStyle:(NSString*)data
}

//NSLog(@"%@", components);
self._textComponent = components;
self._textComponents = components;
self._plainText = data;
}


- (void)parse:(NSString *)data valid_tags:(NSArray *)valid_tags
{
//use to strip the HTML tags from the data
NSScanner *scanner;
NSScanner *scanner = nil;
NSString *text = nil;
NSString *tag = nil;

Expand Down Expand Up @@ -1021,7 +1025,7 @@ - (void)parse:(NSString *)data valid_tags:(NSArray *)valid_tags
}
}

self._textComponent = components;
self._textComponents = components;
self._plainText = data;
//self._plainText = [self._plainText stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
//self._plainText = [self._plainText stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
Expand Down
8 changes: 5 additions & 3 deletions RTLabelProject/Classes/RTLabelProjectAppDelegate.m
Expand Up @@ -66,8 +66,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(


RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake(10,30,300,440)];
[window addSubview:label];
[label release];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15]];

NSMutableDictionary *linkAttributes = [NSMutableDictionary dictionary];
Expand All @@ -81,7 +79,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[selectedLinkAttributes setObject:@"2" forKey:@"underline"];

//NSMutableString *text = [NSMutableString stringWithString:@""];
NSString *text = @"<p indent=0>Lorem <font kern=35 underline=2 style=italic color=blue>ipsum</font> dolor \tsit amet, <i>consectetur adipisicing elit, sed do eiusmod tempor</i> <u color=red>incididunt ut</u> <uu color=green>labore et dolore</uu> magna aliqua.</p><p indent=20><i>Ut enim ad minim</i> veniam, <b>quis nostrud</b> exercitation <font color=#CCFF00 face=HelveticaNeue-CondensedBold size=30>ullamco laboris <i>nisi</i> ut aliquip</font> <font color='blue' size=30 stroke=1>ex ea commodo consequat.</font> Duis aute irure dolor in <font face=Cochin-Bold size=40>reprehenderit</font> <font face=AmericanTypewriter size=20 color=purple>in voluptate velit esse cillum dolore</font> eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat <i>non <font color=cyan>proident,</p> <b>sunt in <u>culpa qui</u> officia</b> deserunt</font> mollit</i> anim id est laborum.\n<p><a href='http://honcheng.com'>clickable link</a></p> ";
NSString *text = @"<p indent=0>!!!Lorem <font kern=35 underline=2 style=italic color=blue>ipsum</font> dolor \tsit amet, <i>consectetur adipisicing elit, sed do eiusmod tempor</i> <u color=red>incididunt ut</u> <uu color=green>labore et dolore</uu> magna aliqua.</p><p indent=20><i>Ut enim ad minim</i> veniam, <b>quis nostrud</b> exercitation <font color=#CCFF00 face=HelveticaNeue-CondensedBold size=30>ullamco laboris <i>nisi</i> ut aliquip</font> <font color='blue' size=30 stroke=1>ex ea commodo consequat.</font> Duis aute irure dolor in <font face=Cochin-Bold size=40>reprehenderit</font> <font face=AmericanTypewriter size=20 color=purple>in voluptate velit esse cillum dolore</font> eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat <i>non <font color=cyan>proident,</p> <b>sunt in <u>culpa qui</u> officia</b> deserunt</font> mollit</i> anim id est laborum.\n<p><a href='http://honcheng.com'>clickable link</a></p> ";
//NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";
//NSString *text = @"Lorem ipsum dolor sit amet";
[label setLinkAttributes:linkAttributes];
Expand All @@ -90,6 +88,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[label setTextAlignment:RTTextAlignmentJustify];
[label setLineSpacing:5];
[label setDelegate:self];

[window addSubview:label];
[label release];


[window setBackgroundColor:[UIColor whiteColor]];

Expand Down

0 comments on commit 4e0e18e

Please sign in to comment.