UIColor의 상수 값을 어떻게 정의합니까?
이런 식으로하고 싶지만 협조적인 구문을 얻을 수 없습니다.
static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];
매크로를 정의 할 수 있다고 생각하지만보기 흉합니다.
나는 카테고리를 사용하여 이런 종류의 새로운 메소드로 클래스를 확장하는 것을 좋아합니다. 다음은 제가 오늘 작성한 코드의 일부입니다.
@implementation UIColor (Extensions)
+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}
+ (UIColor *)aquaColor {
return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}
+ (UIColor *)paleYellowColor {
return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
@end
이제 코드에서 다음과 같은 작업을 수행 할 수 있습니다.
self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];
내가 정의한 색상은 시스템 정의 색상과 잘 어울립니다.
(부수적으로 색상에 더 많은 관심을 기울이면서 RGB보다 HSB 측면에서 더 많이 생각하기 시작했습니다.)
가치 사전 계산에 관한 업데이트 : 내 직감은 가치가 없다는 것입니다. 그러나 정말로 원한다면 정적 변수로 값을 메모 할 수 있습니다.
+ (UIColor *)paleYellowColor {
static UIColor *color = nil;
if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
return color;
}
매크로로 메모를 할 수도 있습니다.
일반적으로 각 프로젝트에 대해 UIColor 범주를 만듭니다.
@interface UIColor (ProjectName)
+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;
@end
구현에서 상수 사용 :
@implementation UIColor (ProjectName)
+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }
@end
필요에 따라 UIFont 및 UIImage에 대해서도 동일하게 수행합니다.
값을 미리 계산하려면 (또는 한 번만 수행) jasoncrawford의 답변을 확장하려면 (이것을 주석으로 입력했지만 주석에 코드 형식을 지정할 수는 없습니다).
+ (UIColor *)paleYellowColor
{
static UIColor* paleYellow = nil;
if (paleYellow == nil)
{
paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
return paleYellow;
}
원래 아이디어가 작동하지 않는 이유는 컴파일러가 일반 코드가 아닌 함수 외부에서만 이니셜 라이저를 사용할 수 있기 때문입니다. 예를 들어 초기화 방법으로 원하는 것을 얻을 수 있습니다.
static UIColor* colorNavBar = nil;
+(void) initialize
{
if (colorNavBar != nil)
{
colorNavBar = ....
}
}
NB const
원래 정의 의 한정자 UIColor
는 어쨌든 불변 이므로 중복 됩니다.
다음과 같이 유사한 CONSTANT를 '정의'할 수 있습니다.
#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]
상수에 익숙한 것처럼 이름으로 부릅니다. FAV_COLOR
도움이 되었기를 바랍니다.
다음과 같이 할 수 있습니다.
#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
Swift에서 : 확장 정의
extension UIColor {
class func XXXWhiteColor() -> UIColor {
return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
}
class func XXXGreenColor() -> UIColor {
return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
}
}
다음과 같이 사용하십시오. Label.background = UIColor.XXXWhiteColor ()
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
myLabel.textColor=textColorApp;
Just define below macro in your constant file and pass only RGB value and use anywhere you want
#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
To use :
[lblCount setTextColor:RGBCOLOR(236, 43, 92)];
I feel it's also worth mentioning another awesome feature that is rarely talked about: Color Literals. Not only are they easier to read, but they are WAY easier to edit. In Swift,
let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha:
When pasted into Xcode, this syntax creates a simple color box. Click here to see an example.
Once you see the box, you can then double click on it to edit it easily. Similarly, you can switch between various IB options, including RGB Sliders, if you have a specific list of color hex values from your designer.
참고URL : https://stackoverflow.com/questions/2718507/how-do-i-define-constant-values-of-uicolor
'Nice programing' 카테고리의 다른 글
http 전송을 위해 git과 함께 socks 프록시 사용 (0) | 2020.12.02 |
---|---|
C #에서 기본 네임 스페이스와 함께 Xpath 사용 (0) | 2020.12.02 |
pg_config 경로를 찾는 방법 (0) | 2020.12.02 |
클래스가 동일한 요소에 클릭 이벤트 리스너 추가 (0) | 2020.12.02 |
onclick 전체 화면으로 이동 (0) | 2020.12.02 |