xcode 将 CGcolorRef 转换为 UIColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10050059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:53:14  来源:igfitidea点击:

Convert CGcolorRef to UIColor

iphoneobjective-ciosxcode

提问by Mc.Lover

I was wondering how can convert CGcolorRefto UIColor? I have created a custom method for my own and I need it to use UIColor

我想知道如何转换CGcolorRefUIColor?我为自己创建了一个自定义方法,我需要它来使用 UIColor

EDITED method :

编辑方法:

    - (void)fontColor:(CGColorRef)colors setString(NSSTring *)blah blah blah  {

    coreTextLayer.foregroundColor = color;


    }


[myClass fontColor:[UIColor redColor] setString....];

the right way is use something like this :

正确的方法是使用这样的东西:

[UIColor redColor]CGColor];

but is there any way to use only UIColor?

但是有没有办法只使用 UIColor?

回答by Conrad Shultz

Does + (UIColor *)colorWithCGColor:(CGColorRef)cgColornot do what you need?

难道+ (UIColor *)colorWithCGColor:(CGColorRef)cgColor不是你所需要的?



Follow-up:

跟进:

Try implementing your method as something like:

尝试将您的方法实现为:

- (void)setFontColor:(UIColor *)color forString(NSString *)aString  {
    [coreTextLayer setForegroundColor:[color CGColor]];
    ...
}

(Adjusted to be more in line with Cocoa naming conventions, and to not use dot syntax. ;-)

(调整为更符合 Cocoa 命名约定,并且不使用点语法。;-)

回答by Xval

Why don't you use

你为什么不使用

  • (UIColor *)initWithCGColor:(CGColorRef)cgColor
  • (UIColor *)initWithCGColor:(CGColorRef)cgColor