How can I create a UIColor
from a hexadecimal string format, such as #00FF00
?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Xcode: Is there a way to change line spacing (UI L
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
There's a nice post on how to tackle the OP's question of extracting a
UIColor
from a hex string. The solution presented below is different from others because it supports string values that may include '0x' or '#' prefixed to the hex string representation... (see usage)Here's the main bit..
Helper method..
Usage:
Complete Reference Article
Swift 2+
I've ported this solution to Swift 2.2. Note that I've changed the
alpha
parameter to an optional value with a default set to 1.0. I've also updated the int type toUInt32
as required by theNSScanner
class in Swift 2.2.Swift 4+
Using the same logic with changes applied for swift 4,
Usage:
I like to ensure the alpha besides the color, so i write my own category
easy to use like this
Another implementation allowing strings like
"FFF"
or"FFFFFF"
and using alpha:Swift equivalent of @Tom's answer, although receiving RGBA Int value to support transparency:
And if you want to be able to use it from string you could use strtoul:
A concise solution: