diff --git a/RTLabelProject/Classes/RTLabel.h b/RTLabelProject/Classes/RTLabel.h index 57546d5..e694d76 100644 --- a/RTLabelProject/Classes/RTLabel.h +++ b/RTLabelProject/Classes/RTLabel.h @@ -66,7 +66,7 @@ typedef enum RTTextAlignment _textAlignment; RTTextLineBreakMode _lineBreakMode; NSString *_plainText; - NSMutableArray *_textComponent; + NSMutableArray *_textComponents; CGSize _optimumSize; CGFloat _lineSpacing; int currentSelectedButtonComponentIndex; diff --git a/RTLabelProject/Classes/RTLabel.m b/RTLabelProject/Classes/RTLabel.m index f365574..d81df9e 100644 --- a/RTLabelProject/Classes/RTLabel.m +++ b/RTLabelProject/Classes/RTLabel.m @@ -39,6 +39,7 @@ @interface RTLabelButton : UIButton { +@private int componentIndex; NSURL *url; } @@ -63,6 +64,7 @@ - (void)dealloc @interface RTLabelComponent : NSObject { +@private NSString *text; NSString *tagLabel; NSMutableDictionary *attributes; @@ -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 @@ -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; } @@ -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; @@ -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; @@ -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; @@ -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"]) @@ -784,7 +788,7 @@ - (void)dealloc delegate = nil; //CFRelease(frame); //CFRelease(framesetter); - [_textComponent release]; + [_textComponents release]; [_plainText release]; [textColor release]; [font release]; @@ -847,7 +851,7 @@ - (void)extractTextStyle:(NSString*)data { //NSLog(@"%@", data); - NSScanner *scanner; + NSScanner *scanner = nil; NSString *text = nil; NSString *tag = nil; @@ -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]; @@ -926,7 +930,7 @@ - (void)extractTextStyle:(NSString*)data } //NSLog(@"%@", components); - self._textComponent = components; + self._textComponents = components; self._plainText = data; } @@ -934,7 +938,7 @@ - (void)extractTextStyle:(NSString*)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; @@ -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:@"<" withString:@"<"]; //self._plainText = [self._plainText stringByReplacingOccurrencesOfString:@">" withString:@">"]; diff --git a/RTLabelProject/Classes/RTLabelProjectAppDelegate.m b/RTLabelProject/Classes/RTLabelProjectAppDelegate.m index 4328fe6..e4d5ea7 100644 --- a/RTLabelProject/Classes/RTLabelProjectAppDelegate.m +++ b/RTLabelProject/Classes/RTLabelProjectAppDelegate.m @@ -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]; @@ -81,7 +79,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [selectedLinkAttributes setObject:@"2" forKey:@"underline"]; //NSMutableString *text = [NSMutableString stringWithString:@""]; - NSString *text = @"

Lorem ipsum dolor \tsit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,

sunt in culpa qui officia deserunt mollit anim id est laborum.\n

clickable link

"; + NSString *text = @"

!!!Lorem ipsum dolor \tsit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,

sunt in culpa qui officia deserunt mollit anim id est laborum.\n

clickable link

"; //NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"; //NSString *text = @"Lorem ipsum dolor sit amet"; [label setLinkAttributes:linkAttributes]; @@ -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]];