Created
November 10, 2011 04:25
-
-
Save Shilo/1354110 to your computer and use it in GitHub Desktop.
A category for Sparrow that allows shadows on a SPTextField.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPTextField+Shadow.h | |
// Sparrow | |
// | |
// Created by Shilo White on 11/9/11. | |
// Copyright (c) 2011 Shilocity Productions. All rights reserved. | |
// | |
#import "SPTextField.h" | |
@interface SPTextField (Shadow) | |
@property (nonatomic, assign, readwrite) BOOL shadow; | |
@property (nonatomic, assign, readwrite) uint shadowColor; | |
@property (nonatomic, assign, readwrite) float shadowX; | |
@property (nonatomic, assign, readwrite) float shadowY; | |
@property (nonatomic, assign, readwrite) float shadowBlur; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPTextField+Shadow.m | |
// Sparrow | |
// | |
// Created by Shilo White on 11/9/11. | |
// Copyright (c) 2011 Shilocity Productions. All rights reserved. | |
// | |
#import "SPTextField+Shadow.h" | |
#import "SPMacros.h" | |
#import <objc/runtime.h> | |
static char sShadow; | |
static char sShadowColor; | |
static char sShadowX; | |
static char sShadowY; | |
static char sShadowBlur; | |
@implementation SPTextField (Shadow) | |
@dynamic shadow; | |
@dynamic shadowColor; | |
@dynamic shadowX; | |
@dynamic shadowY; | |
@dynamic shadowBlur; | |
- (void)setShadow:(BOOL)shadow { | |
if ([self shadow] != shadow) { | |
objc_setAssociatedObject(self, &sShadow, [NSNumber numberWithBool:shadow], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (BOOL)shadow { | |
NSNumber *shadow = objc_getAssociatedObject(self, &sShadow); | |
return [shadow boolValue]; | |
} | |
- (void)setShadowColor:(uint)shadowColor { | |
if ([self shadowColor] != shadowColor) { | |
objc_setAssociatedObject(self, &sShadowColor, [NSNumber numberWithUnsignedInt:shadowColor], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (uint)shadowColor { | |
NSNumber *shadowColor = objc_getAssociatedObject(self, &sShadowColor); | |
return (uint)[shadowColor unsignedIntValue]; | |
} | |
- (void)setShadowX:(float)shadowX { | |
if ([self shadowX] != shadowX) { | |
objc_setAssociatedObject(self, &sShadowX, [NSNumber numberWithFloat:shadowX], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (float)shadowX { | |
NSNumber *shadowX = objc_getAssociatedObject(self, &sShadowX); | |
return [shadowX floatValue]; | |
} | |
- (void)setShadowY:(float)shadowY { | |
if ([self shadowY] != shadowY) { | |
objc_setAssociatedObject(self, &sShadowY, [NSNumber numberWithFloat:shadowY], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (float)shadowY { | |
NSNumber *shadowY = objc_getAssociatedObject(self, &sShadowY); | |
return [shadowY floatValue]; | |
} | |
- (void)setShadowBlur:(float)shadowBlur { | |
if ([self shadowBlur] != shadowBlur) { | |
objc_setAssociatedObject(self, &sShadowBlur, [NSNumber numberWithFloat:shadowBlur], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (float)shadowBlur { | |
NSNumber *shadowBlur = objc_getAssociatedObject(self, &sShadowBlur); | |
return [shadowBlur floatValue]; | |
} | |
- (SPDisplayObject *)createRenderedContents { | |
float width = mHitArea.width; | |
float height = mHitArea.height; | |
float fontSize = mFontSize == SP_NATIVE_FONT_SIZE ? SP_DEFAULT_FONT_SIZE : mFontSize; | |
UILineBreakMode lbm = UILineBreakModeTailTruncation; | |
CGSize textSize = [mText sizeWithFont:[UIFont fontWithName:mFontName size:fontSize] | |
constrainedToSize:CGSizeMake(width, height) lineBreakMode:lbm]; | |
float xOffset = 0; | |
if (mHAlign == SPHAlignCenter) xOffset = (width - textSize.width) / 2.0f; | |
else if (mHAlign == SPHAlignRight) xOffset = width - textSize.width; | |
float yOffset = 0; | |
if (mVAlign == SPVAlignCenter) yOffset = (height - textSize.height) / 2.0f; | |
else if (mVAlign == SPVAlignBottom) yOffset = height - textSize.height; | |
mTextArea.x = xOffset; | |
mTextArea.y = yOffset; | |
mTextArea.width = textSize.width; | |
mTextArea.height = textSize.height; | |
BOOL shadow = self.shadow; | |
CGColorRef baseColorRef = [UIColor colorWithRed:SP_COLOR_PART_RED(mColor)/255.0f green:SP_COLOR_PART_GREEN(mColor)/255.0f blue:SP_COLOR_PART_BLUE(mColor)/255.0f alpha:1.0f].CGColor; | |
CGColorRef shadowColorRef = NULL; | |
if (shadow) { | |
uint shadowColor = [self shadowColor]; | |
shadowColorRef = [UIColor colorWithRed:SP_COLOR_PART_RED(shadowColor)/255.0f green:SP_COLOR_PART_GREEN(shadowColor)/255.0f blue:SP_COLOR_PART_BLUE(shadowColor)/255.0f alpha:1.0f].CGColor; | |
} | |
SPTexture *texture = [[SPTexture alloc] initWithWidth:width height:height | |
scale:[SPStage contentScaleFactor] | |
colorSpace:SPColorSpaceRGBA | |
draw:^(CGContextRef context) | |
{ | |
if (shadow) CGContextSetShadowWithColor(context, CGSizeMake([self shadowX], -[self shadowY]), [self shadowBlur], shadowColorRef); | |
if (!mBorder) | |
{ | |
CGContextSetStrokeColorWithColor(context, baseColorRef); | |
CGContextSetLineWidth(context, 1.0f); | |
CGContextStrokeRect(context, CGRectMake(0.5f, 0.5f, width-1, height-1)); | |
} | |
CGContextSetFillColorWithColor(context, baseColorRef); | |
[mText drawInRect:CGRectMake(0, yOffset, width, height) | |
withFont:[UIFont fontWithName:mFontName size:fontSize] | |
lineBreakMode:lbm alignment:(UITextAlignment)mHAlign]; | |
}]; | |
SPImage *image = [SPImage imageWithTexture:texture]; | |
[texture release]; | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Shilo!
Thanks for this great category :DDD
I added the category to one project, and I realized that if we don't specify the property border, all the SPTextField assume border=YES as default, then I changed the line 119 to: if (mBorder) [removing the "!"] and it seems the problem is gone.
Regards.