UIColor to Hexadecimal (web color)

后端 未结 5 1419
悲&欢浪女
悲&欢浪女 2021-02-15 23:37

Is there an easy way to convert UIColor to a hexadecimal value ?
Or do we have to get the RGB components with CGColorGetComponents and then work it

5条回答
  •  执念已碎
    2021-02-16 00:30

    I would consider using Erica Sadun's UIColor category. It includes a lot of functionality for free, including hex representations. It's pretty easy to use, just add it to whatever class header you're using it in or, add it to the pre-compiled header for ultimate flexibility. If you're adding to the pre-compiled header, do so similar to something like this:

    #ifdef __OBJC__
        #import 
        #import 
        #import "UIColor-Expanded.h"
    #endif
    

    Then You can use it like so NSLog(@"%@", [myColor hexStringFromColor]);

    GitHub link to the UIColor category: https://github.com/erica/uicolor-utilities

    ArsTechnica article about it: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

提交回复
热议问题