Add custom .colorNames to UIColor somehow?
Those aren't really color shortcuts. That's just inferring a type. The fontColor
property is typed as UIColor, and UIColor has a bunch of read-only properties on the class that are color names and return color objects. So when you say
myLabel.fontColor = .myCustomColor
The dot tells it "it is a field somewhere, guess where" and Swift goes, well, I need a UIColor
, so let's look if the UIColor
class has properties of that name that return the right type.
So to add your own, you'd have to define your color properties in an extension on UIColor.
extension UIColor {
static let con_pink = UIColor( red: 1.0, green: 0.0, blue: 0.5, alpha: 1.0 )
}
and those you can use as myLabel.fontColor = .con_pink
.
But note that, if you do this, you're risking collisions with any color methods Apple might add in the future. So I recommend that you add a prefix to the property names (I chose "con_" for you based on your username), to make it less likely that Apple use the same name.
How to define my own custom UIColor?
Try something like this:
extension UIColor {
static let myCustomColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
add uiColor for cgsize (swift3)
You need to use your color setFill() method to define the color that you would like to fill your image context and just use a UIBezierPath to fill the desired area:
let newSize = CGSize(width: 900, height: 900)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
defer { UIGraphicsEndImageContext() }
UIColor.blue.setFill()
UIBezierPath(rect: CGRect(origin: .zero, size: newSize)).fill()
UIGraphicsGetImageFromCurrentImageContext()
Is there a way to store custom colors in a file and access them from code in iOS?
As vokilam suggested, it is always good to create a Category using UIColor
and adding your own custom colors through class methods inside the category. It is quite handy and easy to have a track of colors inside a single class. Something like:
In Xcode, File -> New --> New ---> File... Cocoa Touch -> Objective-C Category -> Next -> Select UIColor in "Category on" list --> Give a name for the category --> Next --> Create
This gives you .h and .m file... In your .h file, add a custom color method like
+(UIColor *)customLightGreenColor;
And in the .m file, you can implement the method something like (You have different options to set your colors here):
+(UIColor *)customlightGreenColor
{
UIColor *lightGreenColor = [UIColor colorWithRed:0.5 green:0.87 blue:0.0 alpha:1.0];
return lightGreenColor;
}
Likewise, you can add any number of colors in your category and use it in your application. Hope this gives you a heads up...
Custom Color Swift
You can set the background color of a view using:
self.view.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 1.00)
In Swift 3, you can use default colors like this: UIColor.blue()
. If you want to customize your color, you can use UIColor with specific red, green, and blue values (or any other number of parameters). You convert a color from HEX to UIColor by using any number of available online tools (like: http://uicolor.xyz/#/hex-to-ui). You can also write a Swift extension to do this in your code.
How to add custom color to navigation bar in iphone?
Using RGB values like this:
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0]];
How can I programmatically obtain all colors included in Color Assets catalog?
Currently there is no way to request the Asset Catalog to programmatically tell you what all assets it contains. i.e. there's no API, imho, that will let you fetch the color names (or other asset names) you have added in the Asset Catalog.
You can, ofcourse, fetch the color/asset if you already know the name.
Furthermore, the asset catalog compiles into Assets.car
which is unreadable so attempts to read that file are also futile.
Anyways... coming back to your case, you would have to manually maintain some mechanism like an Enum
to track the colors.
Solution:
- Add a color to Asset Catalog; say named "WeirdOrange"
Use an
Enum
to maintain your custom color names, like:enum Colors: String {
//... previous cases
case weirdOrange = "WeirdOrange" //... now we add this one
}Use it in the app as:
UIColor(named: Colors.weirdOrange.rawValue)
Example:
Lets fine-tune it a bit more for your purpose:
enum Colors: String, CaseIterable {
case funkyMunky = "FunkyMunky"
case weirdOrange = "WeirdOrange"
case popBlue = "PopBlue"
case murkyPoo = "MurkyPoo"
static var assetCatalogued: [UIColor] {
return self.allCases.compactMap { (colorCase) -> UIColor? in
return UIColor(named: colorCase.rawValue)
}
}
}
for color in Colors.assetCatalogued {
//Do something
print(color)
}
NOTE:
- To run the example, either change the color names or add colors with the given names, else
Colors.assetCatalogued
will not return anyUIColor
s - Used
CaseIterable
for.allCases
- You would have to manually add/remove a case when adding/removing the related color asset
ios Convert integer color to uicolor or hex values?
It appears that the integer number to which you are referencing is a packed integer. See: android color documentation. You need to find a way to convert the packed int to a hex and then you can use this macro (obtained here) which converts a hex number to UIColor:
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 green:((c>>16)&0xFF)/255.0 blue:((c>>8)&0xFF)/255.0 alpha:((c)&0xFF)/255.0]
I know this doesn't really answer your question but it may help you narrow down your search.
Edit
Ok so I just realized the above macro does solve your problem. Just give it the integer representation and it will give you the right UIColor. My test code is below:
UIColor *color = HEXCOLOR(-16776961); // Blue const from android link above
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];
NSLog(@"%@",colorAsString); // Prints 1.0 0.0 0.0 1.0 which corresponds to 0xff0000ff
I had some help from here.
Edit
Fixed the macro to expect the correct RGBA values:
#define ANDROID_COLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:((c)&0xFF)/255.0 alpha:((c>>24)&0xFF)/255.0]
The previous macro expected RGBA while the Android color int gave ARGB.
Swift custom rgb color not shown
It should work
cell.viewAllButton.setTitleColor(UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1.0), for: .normal)
Related Topics
How to Remove Items from an Array When Deselecting a Row in a UItableview
Swift 3 Cocoa: Use Quicklook to Preview File in Os X
Nstimer Scheduledtimerwithtimeinterval and Target Is "Class Level Function"
Realm + Nstableview + Nsarraycontroller
How to Write a Generic Function for Floating Point Values in Swift
How to Import Zbar Framework in Swift Project
Why Is The Swiftui List Not Lazy (Compared to Lazyvstack)
Macos Change Menubar Icon Using Swift 4
Pdf417 Decode and Generate The Same Barcode Using Swift
Changing Associated Value of Enum Swift
Avplayer Won't Let Me Access Url
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent
Arkit: How to Tell If User's Face Is Parallel to Camera
Programmatic Beginrefreshing() on iOS11 Has Problems with Largetitles Mode