You have 4 TextFields, defined with their IBOutlets
@IBOutlet weak var label25: UILabel!
@IBOutlet weak var label50: UILabel!
@IBOutlet weak var label75: UILabel!
@IBOutlet weak var label100: UILabel!
and a textField to enter the value
@IBOutlet weak var inputField: UITextField!
In the IBAction for the button:
@IBAction func calculate(sender: UIButton) {
guard let initialValue = inputField.text, let value = Float(initialValue) else { return }
let value25 = 0.25 * value
let value50 = 0.5 * value
let value75 = 0.75 * value
label25.text = String(value25)
label50.text = String(value50)
label75.text = String(value75)
label100.text = String(value)
}
When you say:
im trying to take 2 label boxes and calculate out into 4 boxes where each box is 25 percent,50 percent,75 percent and 100 percent.
Do you mean you want to calculate the values between the 2 at 25, 50, 75% ?
If so,
You have 2 values : startValue and endValue
@IBOutlet weak var startValueField: UITextField!
@IBOutlet weak var endValueField: UITextField!
change the IBAction
@IBAction func calculate(sender: UIButton) {
guard let startValueText = startValueField.text, let endValueText = endValueField.text, let startValue = Float(startValueText) , let endValue = Float(endValueText) else { return }
let spread = endValue - startValue
let value25 = startValue + 0.25 * spread
let value50 = startValue + 0.5 * spread
let value75 = startValue + 0.75 * spread
label25.text = String(value25)
label50.text = String(value50)
label75.text = String(value75)
label100.text = String(endValue)
}
If you want to add some fun, define the color of text of startValueField as Green ; and color of endValueField as red.
Then, show a progression of colors in the labels you write :
@IBAction func calculate(sender: UIButton) {
guard let startValueText = startValueField.text, let endValueText = endValueField.text, let startValue = Float(startValueText) , let endValue = Float(endValueText) else { return }
var startRed: CGFloat = 0, startGreen: CGFloat = 0, startBlue: CGFloat = 0, startAlpha: CGFloat = 0
var endRed: CGFloat = 0, endGreen: CGFloat = 0, endBlue: CGFloat = 0, endAlpha: CGFloat = 0
let startColor = startValueField.textColor!
let endColor = endValueField.textColor!
startColor.getRed(&startRed, green: &startGreen, blue: &startBlue, alpha: &startAlpha)
endColor.getRed(&endRed, green: &endGreen, blue: &endBlue, alpha: &endAlpha)
let redSpread = endRed - startRed
let greenSpread = endGreen - startGreen
let blueSpread = endBlue - startBlue
let spread = endValue - startValue
let value25 = startValue + 0.25 * spread
let value50 = startValue + 0.5 * spread
let value75 = startValue + 0.75 * spread
var colorRed = startRed + 0.25 * redSpread
var colorGreen = startGreen + 0.25 * greenSpread
var colorBlue = startBlue + 0.25 * blueSpread
var colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
label25.text = String(value25)
label25.textColor = colorLabel
colorRed = startRed + 0.5 * redSpread
colorGreen = startGreen + 0.5 * greenSpread
colorBlue = startBlue + 0.5 * blueSpread
colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
label50.text = String(value50)
label50.textColor = colorLabel
colorRed = startRed + 0.75 * redSpread
colorGreen = startGreen + 0.75 * greenSpread
colorBlue = startBlue + 0.75 * blueSpread
colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
label75.text = String(value75)
label75.textColor = colorLabel
label100.text = String(endValue)
label100.textColor = endValueField.textColor!
}