Setting tintColor for Apple Watch complication - swift

I am trying to set the header text color for a Modular Large complication.
I have already customized the watch face to use Multicolor.
However, when I build and run this code, the header text color is still white (which is the default).
Why isn't the color updating?
private func templateForClassModularLarge(className: Schedule) -> CLKComplicationTemplateModularLargeStandardBody {
let template = CLKComplicationTemplateModularLargeStandardBody()
let headerTextProvider = CLKSimpleTextProvider(text: "My Schedule", shortText: "Schedule")
headerTextProvider.tintColor = UIColor(red: 101, green: 153, blue: 255, alpha: 1)
template.headerTextProvider = headerTextProvider
template.body1TextProvider = CLKTimeIntervalTextProvider(startDate: className.start, endDate: className.end)
template.body2TextProvider = CLKSimpleTextProvider(text: className.description, shortText: className.shortDescription)
return template
}

UIColor parameter types are CGFloat, specified as a value from 0.0 to 1.0.
Because your RGB parameters are greater than 1, the color ends up being white, which would be:
UIColor(red: 1, green: 1, blue: 1, alpha: 1)
To fix this issue, simply change your tintColor to
headerTextProvider.tintColor = UIColor(red: 101/255, green: 153/255, blue: 255/255, alpha: 1)

Related

Creating CGColor from RGB Value

I use the following code to set the background of a viewcontroller
view.wantsLayer = true
let myColor = NSColor(calibratedRed: 50, green: 50, blue: 50, alpha: 1.0)
view.layer?.backgroundColor = myColor.cgColor
But on debugging myColor i get the following color instead of the intended color
Here is the documentation. Anything above 1 is considered as 1. You need to divide each value by 255.
view.wantsLayer = true
let myColor = NSColor(calibratedRed: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
view.layer?.backgroundColor = myColor.cgColor
Also, check this out -> Link
Range of color components (RGBA) vary between [0,1]
init(red:green:blue:alpha:)
Creates a color object with the specified red, green, blue, and alpha
channel values. This method accepts extended color component values.
If the red, green, blue, or alpha values are outside of the 0-1.0
range,
So you can get cgColor
view.layer?.backgroundColor = NSColor(calibratedRed: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 1.0).cgColor

How to programmatically change the background color of the view [duplicate]

I am trying to change the text colour in a UITextField using the following code (RGBA value) however it just appears white, or clear, I'm not too sure as the background is white itself.
passwordTextField.textColor = UIColor(red: CGFloat(202.0), green: CGFloat(228.0), blue: CGFloat(230.0), alpha: CGFloat(100.0))
passwordTextField.returnKeyType = UIReturnKeyType.Done
passwordTextField.placeholder = "Password"
passwordTextField.backgroundColor = UIColor.clearColor()
passwordTextField.borderStyle = UITextBorderStyle.RoundedRect
passwordTextField.font = UIFont(name: "Avenir Next", size: 14)
passwordTextField.textAlignment = NSTextAlignment.Center
passwordTextField.secureTextEntry = true
RGB values for UIColor are between 0 and 1 (see the documentation "specified as a value from 0.0 to 1.0")
You need to divide your numbers by 255:
passwordTextField.textColor = UIColor(red: CGFloat(202.0/255.0), green: CGFloat(228.0/255.0), blue: CGFloat(230.0/255.0), alpha: CGFloat(1.0))
Another thing, you don't need to create CGFloats:
passwordTextField.textColor = UIColor(red:202.0/255.0, green:228.0/255.0, blue:230.0/255.0, alpha:1.0)
Using convenience init ( code like a pro )
Step 1
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
}
}
Usage
//let color = UIColor(red: 202/255, green: 228/255, blue: 230/255, alpha: 1) ☠️
let color = UIColor(r: 202, g: 228, b: 230) // 😍
try this instead :
passwordTextField.textColor = UIColor(red: 0.792, green: 0.894, blue: 0.901, alpha: 1.0
Always put substituted values. 202/255 = 0.792
red, green, blue and alpha are supposed to be between 0.0 and 1.0.
As others mentioned, UIColor components are normalized in the range 0.0 ~ 1.0 (I think wide color gamuts are the exception, but haven't researched that yet).
A conveninet extension to the UIColor class will let you use values in the 0~255 range (like those obtained from various inspectors and image editing tools):
import UIKit
extension UIColor {
convenience init(
redByte red:UInt8,
greenByte green:UInt8,
blueByte blue:UInt8,
alphaByte alpha:UInt8
) {
self.init(
red: CGFloat(red )/255.0,
green: CGFloat(green)/255.0,
blue: CGFloat(blue )/255.0,
alpha: CGFloat(alpha)/255.0
)
}
}
UIColor convenient methods, from Integers, or from Hex.
extension UIColor {
convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
}
convenience init(rgb: Int, alpha: CGFloat = 1) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF,
alpha: alpha
)
}
}

How to Convert a string into a UIColor (Swift)

I have several colors defined in my SKScene:
let color1 = UIColor(red: 1, green: 153/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 74/255, green: 134/255, blue: 232/255, alpha: 1)
let color3 = UIColor(red: 0, green: 0, blue: 1, alpha: 1)
let color4 = UIColor(red: 0, green: 1, blue: 0, alpha: 1)
let color5 = UIColor(red: 153/255, green: 0, blue: 1, alpha: 1)
let color6 = UIColor(red: 1, green: 0, blue: 0 , alpha: 1)
These colors correspond to tiles with different values in my game. The one tile goes with color1 and so on...
When the tiles are added together, I add their values and want to give them a new color according to their value.
So I want the value of the tile to play an effect on what color the tile is.
I have tried:
tile.color = UIColor(named: "color\(value)") ?? color1
But when I do this, it always uses the default value(color1).
How do I make so the value of the tile plays an effect in the color of the tile?
Named UIColors are initalized from color set in xcassets catalog.
You can set color depending on your value
switch value {
case 2: tile.color = color2
...
default: tile.color = color1
}
or you can create color sets in xcassets catalog.
UIColor named: only works when you define a color set asset. Since your colors are defined in code, UIColor named: will never return anything but nil.
One solution is to put your colors into a dictionary instead of separate variables.
let colors: [String: UIColor] = [
"color1" : UIColor(red: 1, green: 153/255, blue: 0, alpha: 1),
"color2" : UIColor(red: 74/255, green: 134/255, blue: 232/255, alpha: 1),
"color3" : UIColor(red: 0, green: 0, blue: 1, alpha: 1),
"color4" : UIColor(red: 0, green: 1, blue: 0, alpha: 1),
"color5" : UIColor(red: 153/255, green: 0, blue: 1, alpha: 1),
"color6" : UIColor(red: 1, green: 0, blue: 0 , alpha: 1),
]
Then you can get your color as:
tile.color = colors["color\(value)"] ?? colors["color1"]!

Does the legend limit the number of titles?

I am using iso-charts to try and draw a pie chart in an iOS app.
No matter what I try, I can't seem to get the full number of titles to draw in the legend.
I have my data set up as:
ages = ["18-", "25-", "35-", "45-", "55-", "65-", "75+"]
agePercentages = [10.0, 20.0, 30.0, 5.0, 10.0, 45.0, 120.0]
My code to setup the pieChart as:
func setPieDefaults(myPieChart: PieChartView) -> PieChartView {
myPieChart.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.width*0.5)
myPieChart.usePercentValuesEnabled = true
myPieChart.holeTransparent = true
myPieChart.holeColor = UIColor.darkPurpleColor()
myPieChart.backgroundColor = UIColor.darkPurpleColor()
myPieChart.rotationAngle = 0.0
myPieChart.rotationEnabled = true
myPieChart.centerTextFont = UIFont(name: "HelveticaNeue-Bold", size:20)!
myPieChart.descriptionText = ""
myPieChart.centerText = "%"
myPieChart.centerTextColor = UIColor.whiteColor()
myPieChart.drawHoleEnabled = true
myPieChart.noDataText = "Loading Data ..."
let legend = myPieChart.legend
legend.font = UIFont(name: "Arial", size: 11)!
legend.textColor = UIColor.whiteColor()
legend.position = .RightOfChart
legend.form = .Circle
return myPieChart
}
And ..
func setChart(dataPoints: [String], values: [Double], myPieView: PieChartView) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
var colors = [UIColor]()
switch myPieView {
case genderPieChartView:
colors = [UIColor.blueColor(), UIColor.pinkColor()]
default:
colors = ChartColorTemplates.colorful()
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: nil)
pieChartDataSet.sliceSpace = 3.0
pieChartDataSet.colors = colors
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
myPieView.animate(xAxisDuration: 2, yAxisDuration: 2)
let pFormatter = NSNumberFormatter()
pFormatter.numberStyle = .PercentStyle
pFormatter.maximumFractionDigits = 0
pFormatter.percentSymbol = ""
pFormatter.multiplier = 1
pieChartData.setValueFormatter(pFormatter)
pieChartData.setValueFont(UIFont(name: "HelveticaNeue-Bold", size: 11)!)
pieChartData.setValueTextColor(UIColor.whiteColor())
myPieView.data = pieChartData
myPieView.drawSliceTextEnabled = false
}
But it won't print any more than 5 of the labels. It's returning the pie-slices perfectly. But not the labels in the legend.
Am I doing something wrong? Thanks in advance for letting me know.
Solved.
In reading the framework docs for the Android version a little better:
The number of entries the automatically generated legend contains
depends on the number of different colors (across all DataSet objects)
as well as on the DataSet labels. The labels of the Legend depend on
the labels set for the used DataSet objects in the chart. If no labels
for the DataSet objects have been specified, the chart will
automatically generate them. If multiple colors are used for one
DataSet, those colors are grouped and only described by one label.
I realised I needed to increase the number of colours in the ChartColorTemplate.colorful() definition to match the number of labels I was trying to use. The current example code comes with only 5 colours defined.
public class func colorful () -> [UIColor]
{
return [
UIColor(red: 193/255.0, green: 37/255.0, blue: 82/255.0, alpha: 1.0),
UIColor(red: 255/255.0, green: 102/255.0, blue: 0/255.0, alpha: 1.0),
UIColor(red: 245/255.0, green: 199/255.0, blue: 0/255.0, alpha: 1.0),
UIColor(red: 106/255.0, green: 150/255.0, blue: 31/255.0, alpha: 1.0),
UIColor(red: 179/255.0, green: 100/255.0, blue: 53/255.0, alpha: 1.0),
UIColor(red: 200/255.0, green: 100/255.0, blue: 53/255.0, alpha: 1.0), // added colour
UIColor(red: 150/255.0, green: 150/255.0, blue: 70/255.0, alpha: 1.0), // added colour
UIColor(red: 84/255.0, green: 78/255.0, blue: 53/255.0, alpha: 1.0), // added colour
]
}
If you have the same problem, then define a lot more colours to meet the needs of the maximum number of labels needed. This will also solve a problem if you try and word-wrap the legend (which I was also facing).

Extra argument in call when using var

I'm trying to use this code:
var alpha : Float
alpha = 0.5
self.view.backgroundColor = UIColor(red: 1, green:0, blue: 0, alpha:alpha)
However, I get the error:
Extra argument 'green' in call
What is wrong with this code? Moreover, why is
self.view.backgroundColor = UIColor(red: 1, green:0, blue: 0, alpha: 0)
working just fine?
Answer was: Swift UIColor initializer - compiler error only when targeting iPhone5s
Use float instead of integers.
UIColor(red: 1.0, green:0.0, blue: 0.0, alpha:alpha)
This also happens when you unwrap the a UIColor instance that wasn't declared as optional.
Instead of:
let brokenColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)!
Use this:
let color: UIColor! = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
If you are using variables use following -
var color: UIColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
My particular iteration of this error happened when I was trying to set the border color of a button, and was getting the "extra argument 'green' in call" error, but once I stored it in a constant the true error arose, which was the constant not being CGColor. So this fixed it.
let borderColor:UIColor = UIColor(red: 23/255, green: 247/255, blue: 252/255, alpha: 1)
loginButton.layer.borderColor = borderColor.CGColor
Put a space after the semicolons in the call
green: 0, alpha: alpha