Permalink
Browse files

Added the ability to use <font color="#AABBFF">text</font> style tags…

… 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...
1 parent 9009c2f commit ded73cb42f5419e741949e414eaf3a2b06c79ebd @AbovegroundDan committed Dec 12, 2011
@@ -37,6 +37,7 @@
#import "CMarkupValueTransformer.h"
#import "CCoreTextRenderer.h"
#import "UIFont_CoreTextExtensions.h"
+#import "UIColor+Hex.h"
@interface CCoreTextLabel ()
@property (readwrite, nonatomic, retain) CCoreTextRenderer *renderer;
@@ -81,6 +82,13 @@ + (NSAttributedString *)normalizeString:(NSAttributedString *)inString font:(UIF
{
[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;
@@ -36,6 +36,8 @@ extern NSString *const kMarkupBoldAttributeName;
extern NSString *const kMarkupItalicAttributeName;
extern NSString *const kMarkupSizeAdjustmentAttributeName;
extern NSString *const kMarkupAttachmentAttributeName;
+extern NSString *const kMarkupTextColorAttributeName;
+
@interface CMarkupValueTransformer : NSValueTransformer
@@ -43,6 +43,7 @@
NSString *const kMarkupItalicAttributeName = @"com.touchcode.italic";
NSString *const kMarkupSizeAdjustmentAttributeName = @"com.touchcode.sizeAdjustment";
NSString *const kMarkupAttachmentAttributeName = @"com.touchcode.attachment";
+NSString *const kMarkupTextColorAttributeName = @"com.touchcode.textColor";
@interface CMarkupValueTransformer ()
@property (readwrite, nonatomic, strong) NSMutableArray *attributesForTags;
@@ -93,6 +94,7 @@ - (id)transformedValue:(id)value error:(NSError **)outError
__block NSMutableDictionary *theTextAttributes = NULL;
__block NSURL *theCurrentLink = NULL;
+ __block NSString *theCurrentColor = NULL;
CSimpleHTMLParser *theParser = [[CSimpleHTMLParser alloc] init];
@@ -105,6 +107,14 @@ - (id)transformedValue:(id)value error:(NSError **)outError
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)
{
NSString *theImageSource = [inAttributes objectForKey:@"src"];
@@ -139,11 +149,12 @@ - (id)transformedValue:(id)value error:(NSError **)outError
};
theParser.closeTagHandler = ^(NSString *inTag, NSArray *tagStack) {
- if ([inTag isEqualToString:@"a"] == YES)
- {
+ if ([inTag isEqualToString:@"a"] == YES|| [inTag isEqualToString:@"font"] == YES)
+ {
theCurrentLink = NULL;
- }
- };
+ theCurrentColor = NULL;
+ }
+ };
theParser.textHandler = ^(NSString *inString, NSArray *tagStack) {
NSDictionary *theAttributes = [self attributesForTagStack:tagStack];
@@ -153,6 +164,12 @@ - (id)transformedValue:(id)value error:(NSError **)outError
{
[theTextAttributes setObject:theCurrentLink forKey:kMarkupLinkAttributeName];
}
+
+ if (theCurrentColor != NULL)
+ {
+ [theTextAttributes setObject:theCurrentColor forKey:kMarkupTextColorAttributeName];
+ }
+
[theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:inString attributes:theTextAttributes]];
};
View
@@ -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
View
@@ -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
@@ -7,6 +7,7 @@
objects = {
/* 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 */; };
4528EAB91454816F00814B3E /* CExampleCoreTextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EAB81454816F00814B3E /* CExampleCoreTextViewController.m */; };
453E8B7E1453255000C8A559 /* MissingImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 453E8B7D1453255000C8A559 /* MissingImage.png */; };
@@ -53,6 +54,8 @@
/* End PBXContainerItemProxy 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>"; };
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>"; };
@@ -151,6 +154,8 @@
45789C5B145F1338004EA63E /* CCoreTextAttachment.h */,
45789C5C145F1338004EA63E /* CCoreTextAttachment.m */,
453E8B7D1453255000C8A559 /* MissingImage.png */,
+ 37850CFF1496640E004AAA31 /* UIColor+Hex.h */,
+ 37850D011496641A004AAA31 /* UIColor+Hex.m */,
);
name = Source;
path = ../Source;
@@ -371,6 +376,7 @@
45920521145A6A9700DECA27 /* CTwitterStyleTweetTextLabel.m in Sources */,
4548A9F6145B2422001C5654 /* CResizerThumb.m in Sources */,
45789C5D145F1338004EA63E /* CCoreTextAttachment.m in Sources */,
+ 37850D021496641A004AAA31 /* UIColor+Hex.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -46,7 +46,7 @@ - (void)viewDidLoad
[self.label2.markupValueTransformer addStyleAttributes:theAttributes forTag:@"username"];
self.label2.lineBreakMode = UILineBreakModeWordWrap;
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";
// #########################################################################

0 comments on commit ded73cb

Please sign in to comment.