[英]Using a UIColor category in iOS 7 and Objective c
I have a UIColor+MyLayout.m file such as: 我有一个UIColor + MyLayout.m文件,例如:
@implementation UIColor (Layout)
- (UIColor *) textBackground
{
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
I have added the .h file to my viewcontroller.m, but how do I call this into a UIColor? 我已将.h文件添加到viewcontroller.m中,但是如何将其称为UIColor?
UIColor *myColor = ? UIColor * myColor =?
Would be better if you do the following: 如果执行以下操作会更好:
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
And then just call it UIColor *myColor = [UIColor textBackground];
然后将其命名为UIColor *myColor = [UIColor textBackground];
Huge screenshot showing coloured text 巨大的屏幕截图显示彩色文本
TRY THIS.... IT IS WORKING!!!! 请尝试...。它正在工作!!!!
1. MAke the subclass of UIColor
named vv. 1.使UIColor
的子类名为vv。
So, in UIColor+vv.h 因此,在UIColor + vv.h中
#import <UIKit/UIKit.h>
@interface UIColor (vv)
+(UIColor*)mh;
@end
UIColor+vv.m UIColor + vv.m
#import "UIColor+vv.h"
@implementation UIColor (vv)
+(UIColor*)mh
{
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
ViewController.m ViewController.m
#import "UIColor+vv.h"
- (void)viewDidLoad
{
lbl.textColor=[UIColor mh];
}
LET me know if you have any problem. 让我知道您是否有任何问题。
You should make this method static like... 您应该将此方法设为静态,例如...
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
And then just call it using class name like 然后像这样使用类名调用它
UIColor *myColor = [UIColor textBackground];
You should import UIColor+MyLayout.h like 您应该像这样导入UIColor + MyLayout.h
#import UIColor+MyLayout.h
您可以通过在签名中使用+
而不是-
,然后简单地将textBackground
转换为类方法:
UIColor *myColor = [UIColor textBackground];
First you have to import your category file in your class like: 首先,您必须将类别文件导入类中,例如:
#import "UIColor+Layout.h"
Then you need to call this method as 然后您需要将此方法称为
[UIColor textBackground]
Also you need to make your category method as a class method 另外,您需要将类别方法作为类方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.