TextColor of UILabel subclass - ios

I'm making button changing style of needed labels, so I created my own class and using appearance() on that, but it is not working. What should I do to fix that?
I've tried the same what I've seen with UILabel class, but made my own subclass:
#IBOutlet weak var SomeLabel: MyUILabel
class MyUILabel: UILabel {}
override func viewDidLoad() {
super.viewDidLoad()
MyUILabel.appearance().textColor = UIColor.red
}
I expected to change color of all labels of class MyUILabel, but it doesn't work, only if I do this with common UILabel.

Do below and this works. I have tested this on simulator.
MyUILabel
import UIKit
class MyUILabel : UILabel {
static func setCustomColor(color: UIColor) {
UILabel.appearance().textColor = color
}
}
In ViewDidLoad have below
override func viewDidLoad() {
super.viewDidLoad()
MyUILabel.setCustomColor(color: UIColor.purple)
}

Related

Swift - UIView Delegate Protocol not returning value

I am trying to apply the colour picker from the question below.
Simple swift color picker popover (iOS)
The way I set this up was the colour picker class is attached to a UIView within the main view controller. The code by Michael Ros works but when I try to access it using my main view controller nothing happens. Below is the code I use in my view controller. Is this correct? I went over other questions and I am not sure what I am doing wrong.
class ViewController: UIViewController {
weak var colorPickerDelegate: ColorPickerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.colorPickerDelegate = self
}
}
}
extension ViewController: colorPickerDelegate {
func colorChanged(color: UIColor) {
print(color)
}
}
The color picker code can be found on the attached question as I wasn't sure if it was allowed to copy the code over.
Thanks for the help
You should sublcass UIView and assign it to the view controllers view, then set the delegate of the ColorPickerView to the view controller:
ColorPickerView.swift
class ColorPickerView : UIView {
weak var delegate: ColorPickerDelegate?
// Other code from Michael Ros's adaptation.
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
lazy var colorPickerView = ColorPickerView(frame: CGRect(origin: .zero, size: CGSize(width: self.view.frame.width, height: 300)))
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.colorPickerView.delegate = self
self.view.addSubview(colorPickerView)
}
}
extension ViewController: ColorPickerDelegate {
func colorDidChange(color: UIColor) {
print(color)
}
}
you should replace
colorChanged -> colorDidChange !
if you delegate is ColorPickerDelegate; I think so

How to initialize ui elements in my ViewController extension in ios, swift

I have a view controller which has a programmatically created label like below.
class MyController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupUI()
}
func setupUI() {
// added an setup the view constraints.
}
}
This works properly. Then I tried to move all the UI element of the view controller to it's extension by creating a extension like below :
private extension MyController {
var label: UILabel = {
**// tried to initialize the label like this. but getting cannot use stored property in extension error**
}()
// then tried like below
var mylabel: UILabel! {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Hello"
return label
}
func setupUI() {
// with the second option I getting nil value error.
}
}
How can I initialize UI elements in viewcontroller extentions programmatically to access internally.
Try this:
uielements.swift
extension MyController {
static let myLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Hello"
return label
}()
}
myController.swift
class MyController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
private func setupUI() {
view.addSubview(RootVC.myLabel)
// Add layout constraints here
}
}
As the error says you are not allowed to declare variables in extension.
You mention that you want to initialize the UI element in the
extensions. of course you can do that. But, what you have in the
extension is a declaration not only initialization.
Have the declaration in the controller and the initialization in an extension method.

Initialise font property at IBOutlet

Is there a way to pre-set the text property of a label at it's outlet? below is what I have in mind but it doesnt work because it is the wrong syntax
#IBOutlet weak var commentHeaderLbl: UILabel! {
didSet {
self.font = UIFont.systemFontOfSize(8)
}
}
If your label is in a UIViewController subclass, you could just do this at viewDidLoad.
If your label is in a UITableViewCell subclass, you could just do this at awakeFromNib.
Example:
override func awakeFromNib() {
super.awakeFromNib()
self.commentHeaderLbl.font = UIFont.systemFontOfSize(8)
}
If You are using interface builder, it is possible to define the font size there too.
Try this:
#IBOutlet weak var commentHeaderLbl: UILabel! {
didSet {
self.font = UIFont.systemFontOfSize(8, weight: UIFontWeightThin)
}
}
you can change the weight property as per your need.

Create many UISliders that update a related label using one function

I have an experience where a user is rating a product, they do this by dragging sliders. Each slider has a related UILabel for the title and a UILabel for the value. I would like to avoid creating 12 functions, one for each slider and the associated label. I am new to development generally. I am guessing a class or an Array would be useful here, but am not sure how to use either. Here's the code that just updates the one value, and I know why that is, I'm just hoping to avoid declaring 12 variables for the value and 12 functions for each one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var slider1: UISlider!
#IBOutlet weak var value1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
value1.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Well,you can use Outlet Collection and 'tag',
Drag every label into a same Outlet Collection
Drag every Slider IBAction into a same function
Then set the tag of slider as the index of label in Outlet Collection.
For example,you first drag labelA to Collection,then the tag is 0
Then all the code
import UIKit
class ViewController: UIViewController {
#IBOutlet var labels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
let index = sender.tag
let label = labels[index]
label.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You can connect more than one slider to the same function. That's the purpose of the sender argument. You can do a switch statement on the pointer in swift, or by a set tag of the slider if you prefer. If it's all updating the same label, why even care which slider is updating?
I can think of two ways of doing this. Make a custom view controller that looks exactly the same as the code you have. It will have the slider and label as subviews. Then you'd only have 12 container views on your storyboard (not the greatest).
The other way would be using IBOutletCollections. These are what they sound like, collections of IBOutlets. Assign each slider that you place on the storyboard a unique tag from 0-11 (this is done under the attributes inspector). The tag will act as an index for the values array. Make sure that when you add each label to the collection, you do it in the correct order (it does matter!). Using IBOutletCollections, your code would look like this:
class ViewController: UIViewController {
#IBOutlet weak var sliders: [UISlider]!
#IBOutlet weak var values: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
values[sender.tag].text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

How do you run a section of code when the user taps the UITextView in Swift?

So I have a UITextView and some placeholder text inside. When the user taps inside the the view, I want to execute some code, i.e. clear the placeholder text. I was trying to create an IBAction but it won't let me. I looked it up online and found this UITextViewDelegate Protocol Reference but I can't figure out how to use it. A lot of the examples I've found for working with delegates are Objective-C and I am working in Swift.
Sorry for the simple question I'm new at this.
Thanks!
Given an IBOutlet to a text view someTextView, all you need to do is make your class conform to UITextViewDelegate, set that text view's delegate to self, and implement the textViewDidBeginEditing method:
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var someTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
someTextView.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
println("Some code")
}
}
The View Controller should adhere to UITextViewDelegate. Then make sure to implement textViewDidBeginEditing delegate methods. The below code should clear the default place holder text when the user starts editing the textview.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(textView: UITextView) {
self.textView.text = ""
}
}

Resources