'2009/10'에 해당되는 글 1건
2009/10/20 12:05
[프로그래밍]
UINavigationBar는 tintColor를 이용해서 색상을 바꿀 수 있다. 하지만, 제목이나 버튼의 글자 색깔을 바꾸는 기능은 제공하지 않고 있다. 이건 참 곤란하다. tintColor를 밝은 색으로 주면 제목과 버튼 글자가 잘 보이지 않는다.
비록 정상적인 방법은 아니지만, 글자 색깔을 바꾸어 보았다.
#include <objc/runtime.h>
#include <objc/message.h>
@implementation UIKitHack (UINavigationTextColor)
/*
* -[UINavigationItemView drawText:inRect:]
*
* 이 메소드를 갈아치워서 UINavigationBar의 Title Text Color를 바꿀 수 있다.
*/
static void UINavigationItemView_drawTextInRect(id self, SEL _cmd, NSString *string, CGRect rect)
{
CGPoint point = rect.origin;
UIFont *font = [self performSelector:@selector(_defaultFont)];
/*
* Draw shadow of string
*/
point.y += 2;
[[UIColor whiteColor] set];
[string drawAtPoint:point forWidth:rect.size.width withFont:font lineBreakMode:UILineBreakModeTailTruncation];
/*
* Draw string
*/
point.y -= 1;
[[UIColor blackColor] set];
[string drawAtPoint:point forWidth:rect.size.width withFont:font lineBreakMode:UILineBreakModeTailTruncation];
}
/*
* -[UIButtonLabel textColor]
*
* 이 메소드를 추가하여 Button Text Color를 바꿀 수 있다.
*/
static UIColor *UIButtonLabel_textColor(id self, SEL _cmd)
{
if ([[self superview] isKindOfClass:NSClassFromString(@"UINavigationButton")])
{
return [UIColor blackColor];
}
else
{
struct objc_super super = { self, [UILabel class] };
return objc_msgSendSuper(&super, _cmd);
}
}
/*
* -[UIButtonLabel shadowColor]
*
* 이 메소드를 추가하여 Button Text Shadow Color를 바꿀 수 있다.
*/
static UIColor *UIButtonLabel_shadowColor(id self, SEL _cmd)
{
if ([[self superview] isKindOfClass:NSClassFromString(@"UINavigationButton")])
{
return [UIColor whiteColor];
}
else
{
struct objc_super super = { self, [UILabel class] };
return objc_msgSendSuper(&super, _cmd);
}
}
/*
* -[UIButtonLabel setShadowOffset:]
*
* 이 메소드를 갈아치워서 Button Text Shadow Offset을 바꿀 수 있다.
*/
static IMP UIButtonLabel_original_setShadowOffset;
static void UIButtonLabel_setShadowOffset(id self, SEL _cmd, CGSize offset)
{
if ([[self superview] isKindOfClass:NSClassFromString(@"UINavigationButton")])
{
struct objc_super super = { self, [UILabel class] };
objc_msgSendSuper(&super, _cmd, CGSizeMake(0, 1));
}
else
{
UIButtonLabel_original_setShadowOffset(self, _cmd, offset);
}
}
/*
* 추가하고 갈아치우는 삽질의 마무리
*/
+ (void)load
{
Class class;
Method method;
class = NSClassFromString(@"UINavigationItemView");
if (class)
{
method = class_getInstanceMethod(class, @selector(drawText:inRect:));
if (method)
{
method_setImplementation(method, (IMP)UINavigationItemView_drawTextInRect);
}
else
{
NSLog(@"-[UINavigationItemView drawText:inRect:] method not found");
}
}
else
{
NSLog(@"UINavigationItemView class not found");
}
class = NSClassFromString(@"UIButtonLabel");
if (class)
{
class_addMethod(class, @selector(textColor), (IMP)UIButtonLabel_textColor, "@@:");
class_addMethod(class, @selector(shadowColor), (IMP)UIButtonLabel_shadowColor, "@@:");
method = class_getInstanceMethod(class, @selector(setShadowOffset:));
if (method)
{
UIButtonLabel_original_setShadowOffset = method_setImplementation(method, (IMP)UIButtonLabel_setShadowOffset);
}
else
{
NSLog(@"-[UIButtonLabel setShadowOffset:] method not found");
}
}
else
{
NSLog(@"UIButtonLabel class not found");
}
}
@end
정상적인 방법이 아니므로, 사용 및 응용은 알아서 하시기를...
이 글은 스프링노트에서 작성되었습니다.


