Skip to content

Commit

Permalink
Added the ability to use <font color="#AABBFF">text</font> style tags…
Browse files Browse the repository at this point in the history
… to color text dynamically without having to set a markup style tag. Useful for dynamic text loaded through network. Uses UIColor+Hex category, which I did NOT write, but the snippet was found long ago, and don't recall where, else I would attribute it.
  • Loading branch information
AbovegroundDan committed Dec 12, 2011
1 parent 9009c2f commit ded73cb
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Source/CCoreTextLabel.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import "CMarkupValueTransformer.h" #import "CMarkupValueTransformer.h"
#import "CCoreTextRenderer.h" #import "CCoreTextRenderer.h"
#import "UIFont_CoreTextExtensions.h" #import "UIFont_CoreTextExtensions.h"
#import "UIColor+Hex.h"


@interface CCoreTextLabel () @interface CCoreTextLabel ()
@property (readwrite, nonatomic, retain) CCoreTextRenderer *renderer; @property (readwrite, nonatomic, retain) CCoreTextRenderer *renderer;
Expand Down Expand Up @@ -81,6 +82,13 @@ + (NSAttributedString *)normalizeString:(NSAttributedString *)inString font:(UIF
{ {
[theMutableText addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:(__bridge id)theColor.CGColor range:range]; [theMutableText addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:(__bridge id)theColor.CGColor range:range];
} }
// [DW]
if ([attrs objectForKey:kMarkupTextColorAttributeName] != NULL)
{
NSString *theColorStr = [attrs objectForKey:kMarkupTextColorAttributeName];
if(theColorStr)
[theMutableText addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:(__bridge id)[UIColor colorWithHexString:theColorStr].CGColor range:range];
}
}]; }];


CTTextAlignment theTextAlignment; CTTextAlignment theTextAlignment;
Expand Down
2 changes: 2 additions & 0 deletions Source/CMarkupValueTransformer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern NSString *const kMarkupBoldAttributeName;
extern NSString *const kMarkupItalicAttributeName; extern NSString *const kMarkupItalicAttributeName;
extern NSString *const kMarkupSizeAdjustmentAttributeName; extern NSString *const kMarkupSizeAdjustmentAttributeName;
extern NSString *const kMarkupAttachmentAttributeName; extern NSString *const kMarkupAttachmentAttributeName;
extern NSString *const kMarkupTextColorAttributeName;



@interface CMarkupValueTransformer : NSValueTransformer @interface CMarkupValueTransformer : NSValueTransformer


Expand Down
25 changes: 21 additions & 4 deletions Source/CMarkupValueTransformer.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
NSString *const kMarkupItalicAttributeName = @"com.touchcode.italic"; NSString *const kMarkupItalicAttributeName = @"com.touchcode.italic";
NSString *const kMarkupSizeAdjustmentAttributeName = @"com.touchcode.sizeAdjustment"; NSString *const kMarkupSizeAdjustmentAttributeName = @"com.touchcode.sizeAdjustment";
NSString *const kMarkupAttachmentAttributeName = @"com.touchcode.attachment"; NSString *const kMarkupAttachmentAttributeName = @"com.touchcode.attachment";
NSString *const kMarkupTextColorAttributeName = @"com.touchcode.textColor";


@interface CMarkupValueTransformer () @interface CMarkupValueTransformer ()
@property (readwrite, nonatomic, strong) NSMutableArray *attributesForTags; @property (readwrite, nonatomic, strong) NSMutableArray *attributesForTags;
Expand Down Expand Up @@ -93,6 +94,7 @@ - (id)transformedValue:(id)value error:(NSError **)outError


__block NSMutableDictionary *theTextAttributes = NULL; __block NSMutableDictionary *theTextAttributes = NULL;
__block NSURL *theCurrentLink = NULL; __block NSURL *theCurrentLink = NULL;
__block NSString *theCurrentColor = NULL;


CSimpleHTMLParser *theParser = [[CSimpleHTMLParser alloc] init]; CSimpleHTMLParser *theParser = [[CSimpleHTMLParser alloc] init];


Expand All @@ -105,6 +107,14 @@ - (id)transformedValue:(id)value error:(NSError **)outError
theCurrentLink = [NSURL URLWithString:theURLString]; theCurrentLink = [NSURL URLWithString:theURLString];
} }
} }
else if ([inTag isEqualToString:@"font"] == YES)
{
NSString *theURLString = [inAttributes objectForKey:@"color"];
if ((id)theURLString != [NSNull null] && theURLString.length > 0)
{
theCurrentColor = [NSString stringWithString:theURLString];
}
}
else if ([inTag isEqualToString:@"img"] == YES) else if ([inTag isEqualToString:@"img"] == YES)
{ {
NSString *theImageSource = [inAttributes objectForKey:@"src"]; NSString *theImageSource = [inAttributes objectForKey:@"src"];
Expand Down Expand Up @@ -139,11 +149,12 @@ - (id)transformedValue:(id)value error:(NSError **)outError
}; };


theParser.closeTagHandler = ^(NSString *inTag, NSArray *tagStack) { theParser.closeTagHandler = ^(NSString *inTag, NSArray *tagStack) {
if ([inTag isEqualToString:@"a"] == YES) if ([inTag isEqualToString:@"a"] == YES|| [inTag isEqualToString:@"font"] == YES)
{ {
theCurrentLink = NULL; theCurrentLink = NULL;
} theCurrentColor = NULL;
}; }
};


theParser.textHandler = ^(NSString *inString, NSArray *tagStack) { theParser.textHandler = ^(NSString *inString, NSArray *tagStack) {
NSDictionary *theAttributes = [self attributesForTagStack:tagStack]; NSDictionary *theAttributes = [self attributesForTagStack:tagStack];
Expand All @@ -153,6 +164,12 @@ - (id)transformedValue:(id)value error:(NSError **)outError
{ {
[theTextAttributes setObject:theCurrentLink forKey:kMarkupLinkAttributeName]; [theTextAttributes setObject:theCurrentLink forKey:kMarkupLinkAttributeName];
} }

if (theCurrentColor != NULL)
{
[theTextAttributes setObject:theCurrentColor forKey:kMarkupTextColorAttributeName];
}



[theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:inString attributes:theTextAttributes]]; [theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:inString attributes:theTextAttributes]];
}; };
Expand Down
15 changes: 15 additions & 0 deletions Source/UIColor+Hex.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// UIColor+Hex.h
// CultureShelf
//
// Created by Daniel Wyszynski on 11/1/11.
// Copyright (c) 2011 Punch Media, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UIColor (i7HexColor)

+ (UIColor *)colorWithHexString:(NSString *)hexString;

@end
49 changes: 49 additions & 0 deletions Source/UIColor+Hex.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// UIColor+Hex.m
// CultureShelf
//
// Created by Daniel Wyszynski on 11/1/11.
// Copyright (c) 2011 Punch Media, Inc. All rights reserved.
//

#import "UIColor+Hex.h"

@implementation UIColor (i7HexColor)

+ (UIColor *)colorWithHexString:(NSString *)hexString {

/* convert the string into a int */
unsigned int colorValueR,colorValueG,colorValueB,colorValueA;
NSString *hexStringCleared = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
if(hexStringCleared.length == 3) {
/* short color form */
/* im lazy, maybe you have a better idea to convert from #fff to #ffffff */
hexStringCleared = [NSString stringWithFormat:@"%@%@%@%@%@%@", [hexStringCleared substringWithRange:NSMakeRange(0, 1)],[hexStringCleared substringWithRange:NSMakeRange(0, 1)],
[hexStringCleared substringWithRange:NSMakeRange(1, 1)],[hexStringCleared substringWithRange:NSMakeRange(1, 1)],
[hexStringCleared substringWithRange:NSMakeRange(2, 1)],[hexStringCleared substringWithRange:NSMakeRange(2, 1)]];
}
if(hexStringCleared.length == 6) {
hexStringCleared = [hexStringCleared stringByAppendingString:@"ff"];
}

/* im in hurry ;) */
NSString *red = [hexStringCleared substringWithRange:NSMakeRange(0, 2)];
NSString *green = [hexStringCleared substringWithRange:NSMakeRange(2, 2)];
NSString *blue = [hexStringCleared substringWithRange:NSMakeRange(4, 2)];
NSString *alpha = [hexStringCleared substringWithRange:NSMakeRange(6, 2)];

[[NSScanner scannerWithString:red] scanHexInt:&colorValueR];
[[NSScanner scannerWithString:green] scanHexInt:&colorValueG];
[[NSScanner scannerWithString:blue] scanHexInt:&colorValueB];
[[NSScanner scannerWithString:alpha] scanHexInt:&colorValueA];


return [UIColor colorWithRed:((colorValueR)&0xFF)/255.0
green:((colorValueG)&0xFF)/255.0
blue:((colorValueB)&0xFF)/255.0
alpha:((colorValueA)&0xFF)/255.0];


}

@end
6 changes: 6 additions & 0 deletions Test/CoreText.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = { objects = {


/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
37850D021496641A004AAA31 /* UIColor+Hex.m in Sources */ = {isa = PBXBuildFile; fileRef = 37850D011496641A004AAA31 /* UIColor+Hex.m */; };
4528EA61145386F500814B3E /* NSAttributedString_DebugExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */; }; 4528EA61145386F500814B3E /* NSAttributedString_DebugExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */; };
4528EAB91454816F00814B3E /* CExampleCoreTextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EAB81454816F00814B3E /* CExampleCoreTextViewController.m */; }; 4528EAB91454816F00814B3E /* CExampleCoreTextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EAB81454816F00814B3E /* CExampleCoreTextViewController.m */; };
453E8B7E1453255000C8A559 /* MissingImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 453E8B7D1453255000C8A559 /* MissingImage.png */; }; 453E8B7E1453255000C8A559 /* MissingImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 453E8B7D1453255000C8A559 /* MissingImage.png */; };
Expand Down Expand Up @@ -53,6 +54,8 @@
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */


/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
37850CFF1496640E004AAA31 /* UIColor+Hex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+Hex.h"; sourceTree = "<group>"; };
37850D011496641A004AAA31 /* UIColor+Hex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+Hex.m"; sourceTree = "<group>"; };
4528EA5F145386F500814B3E /* NSAttributedString_DebugExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSAttributedString_DebugExtensions.h; sourceTree = "<group>"; }; 4528EA5F145386F500814B3E /* NSAttributedString_DebugExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSAttributedString_DebugExtensions.h; sourceTree = "<group>"; };
4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSAttributedString_DebugExtensions.m; sourceTree = "<group>"; }; 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSAttributedString_DebugExtensions.m; sourceTree = "<group>"; };
4528EAB71454816F00814B3E /* CExampleCoreTextViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CExampleCoreTextViewController.h; sourceTree = "<group>"; }; 4528EAB71454816F00814B3E /* CExampleCoreTextViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CExampleCoreTextViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -151,6 +154,8 @@
45789C5B145F1338004EA63E /* CCoreTextAttachment.h */, 45789C5B145F1338004EA63E /* CCoreTextAttachment.h */,
45789C5C145F1338004EA63E /* CCoreTextAttachment.m */, 45789C5C145F1338004EA63E /* CCoreTextAttachment.m */,
453E8B7D1453255000C8A559 /* MissingImage.png */, 453E8B7D1453255000C8A559 /* MissingImage.png */,
37850CFF1496640E004AAA31 /* UIColor+Hex.h */,
37850D011496641A004AAA31 /* UIColor+Hex.m */,
); );
name = Source; name = Source;
path = ../Source; path = ../Source;
Expand Down Expand Up @@ -371,6 +376,7 @@
45920521145A6A9700DECA27 /* CTwitterStyleTweetTextLabel.m in Sources */, 45920521145A6A9700DECA27 /* CTwitterStyleTweetTextLabel.m in Sources */,
4548A9F6145B2422001C5654 /* CResizerThumb.m in Sources */, 4548A9F6145B2422001C5654 /* CResizerThumb.m in Sources */,
45789C5D145F1338004EA63E /* CCoreTextAttachment.m in Sources */, 45789C5D145F1338004EA63E /* CCoreTextAttachment.m in Sources */,
37850D021496641A004AAA31 /* UIColor+Hex.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down
2 changes: 1 addition & 1 deletion Test/CoreText/CExampleCoreTextViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (void)viewDidLoad
[self.label2.markupValueTransformer addStyleAttributes:theAttributes forTag:@"username"]; [self.label2.markupValueTransformer addStyleAttributes:theAttributes forTag:@"username"];
self.label2.lineBreakMode = UILineBreakModeWordWrap; self.label2.lineBreakMode = UILineBreakModeWordWrap;
self.label2.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6]; self.label2.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
self.label2.markup = @"<username>@schwa</username> RUBBERS"; self.label2.markup = @"<font color=\"#33ff00\">colored</font><username>@schwa</username> RUBBERS";


// ######################################################################### // #########################################################################


Expand Down

0 comments on commit ded73cb

Please sign in to comment.