Created
July 3, 2012 01:55
-
-
Save ttsubono/3037036 to your computer and use it in GitHub Desktop.
ボタンの背景色をプログラムで変える方法 Changing the background color of UIButton without a image file
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
/** 使い方 sample usage */ | |
#define BUTTON_RADIUS 10.0f | |
#define BUTTON_BORDER_WIDTH 1.0f | |
NSString *title = @"something"; | |
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; | |
button.frame = rect; | |
[button setTitle:title forState:UIControlStateNormal]; | |
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; | |
[button setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:0.5] forState:UIControlStateNormal radius:BUTTON_RADIUS]; | |
button.layer.borderColor = [UIColor whiteColor].CGColor; | |
button.layer.borderWidth = BUTTON_BORDER_WIDTH; | |
button.layer.cornerRadius = BUTTON_RADIUS; | |
/** 以下、必要クラス Required Classes are as follows: */ | |
// | |
// UIButton+MyCategory.h | |
// | |
#import <Foundation/Foundation.h> | |
@interface UIButton (MyCategory) | |
- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state radius:(CGFloat)radius; | |
- (void)setBackgroundColorString:(NSString *)colorStr forState:(UIControlState)state radius:(CGFloat)radius; | |
@end | |
// | |
// UIButton+MyCategory.m | |
// | |
#import "UIButton+MyCategory.h" | |
#import <QuartzCore/QuartzCore.h> | |
#import "UIColor+MyCategory.h" | |
@implementation UIButton (MyCategory) | |
/** | |
* 特定のStateのボタンの背景色を変える | |
* change UIButton background color for the specified button state | |
* e.g. [buttonObj setBackgroundColorString:[UIColor redColor] forState:UIControlStateNormal radius:10.0f]; | |
* | |
* @param color 新しい背景色 | |
* @param state 適用State | |
* @param radius 角丸 | |
*/ | |
- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state radius:(CGFloat)radius { | |
// imageを生成して、setBackgroundImageにセットしている | |
// create images dynamically, then setBackgroundImage | |
UIView *view = [[UIView alloc] initWithFrame:self.bounds]; | |
view.layer.cornerRadius = radius; | |
view.clipsToBounds = YES; | |
view.backgroundColor = color; | |
UIGraphicsBeginImageContext(self.frame.size); | |
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
[self setBackgroundImage:image forState:state]; | |
[view release]; | |
} | |
/** | |
* 特定のStateのボタンの背景色をRGB文字列で指定する | |
* change UIButton background color for the specified button state | |
* e.g. [buttonObj setBackgroundColorString:@"#123456" forState:UIControlStateNormal radius:10.0f]; | |
* | |
* @param colorStr 新しい背景色 | |
* @param state 適用State | |
* @param radius 角丸 | |
*/ | |
- (void)setBackgroundColorString:(NSString *)colorStr forState:(UIControlState)state radius:(CGFloat)radius { | |
UIColor *color = [UIColor colorWithHexString:colorStr]; | |
[self setBackgroundColor:color forState:state radius:radius]; | |
} | |
@end | |
// | |
// UIColor+MyCategory.h | |
// | |
#import <Foundation/Foundation.h> | |
/** 色をRGB指定 */ | |
@interface UIColor (MyCategory) | |
+ (UIColor *)colorWithHex:(uint)rgbValue; | |
+ (UIColor *)colorWithHexString:(NSString *)str; | |
@end | |
// | |
// UIColor+MyCategory.m | |
// | |
#import "UIColor+MyCategory.h" | |
@implementation UIColor (MyCategory) | |
/** | |
* RGB値からUIColorを生成する | |
* Create UIColor instance from a RGB value | |
* e.g. [UIColor colorWithHex:0x123456] | |
* | |
* @param rgbValue RGB値 | |
* @return RGB値から生成した[UIColor]オブジェクト | |
*/ | |
+ (UIColor *)colorWithHex:(uint)rgbValue { | |
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 | |
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 | |
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]; | |
} | |
/** | |
* RGB文字列からUIColorを生成する | |
* Create UIColor instance from a RGB string | |
* e.g. [UIColor colorWithHexString:@"#123456"] | |
* | |
* @param str RGB文字列 | |
* @return RGB文字列から生成した[UIColor]オブジェクト | |
*/ | |
+ (UIColor *)colorWithHexString:(NSString *)str { | |
const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding]; | |
long x = strtol(cStr+1, NULL, 16); | |
return [UIColor colorWithHex:x]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References in English
http://stackoverflow.com/questions/2808888/is-it-even-possible-to-change-a-uibuttons-background-color
http://pastie.org/830884
References in Japanese
http://d.hatena.ne.jp/masatoshisw20/20111018/1318972544
http://d.hatena.ne.jp/masatoshisw20/20090514/1242272735