Skip to content

Instantly share code, notes, and snippets.

@ttsubono
Created July 3, 2012 01:55
Show Gist options
  • Save ttsubono/3037036 to your computer and use it in GitHub Desktop.
Save ttsubono/3037036 to your computer and use it in GitHub Desktop.
ボタンの背景色をプログラムで変える方法 Changing the background color of UIButton without a image file
/** 使い方 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