Initialise font property at IBOutlet - ios

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.

Related

TextColor of UILabel subclass

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)
}

Set property of IBOutlet

I created an outlet by draggin & dropping an UIImageView into the ViewController.swift file
#IBOutlet weak var imageView: UIImageView!
I want to set the property of imageView to
imageView.contentMode = .scaleToFill
imageView.clipsToBounds = true
Doing this
#IBOutlet weak var imageView: UIImageView! {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
XCode complains that
#IBOutlet attribute requires property to be mutable
How do I go on about this?
What you're trying to do is a perfectly acceptable way to set up an #IBOutlet's properties… just don't reference other #IBOutlets, or the main view, etc, as this may force the main view to load earlier than expected.
You just need to wrap your code inside a didSet block…
#IBOutlet weak var imageView: UIImageView! {
didSet {
imageView.contentMode = .scaleToFill
imageView.clipsToBounds = true
}
}
An outlet is basically just a link to that UI element in the storyboard.
So you link your UIImageView in the storyboard to an element in your UIViewController code and then you can access and change properties of that element once it is loaded.
The Problem
This is the wrong syntax for doing this:
#IBOutlet weak var imageView: UIImageView! {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
The reason for the following error
#IBOutlet attribute requires property to be mutable
is.. having a closure after a property declaration acts as a getter. As an example I can do this
let two = 2
let three = 3
var five: Int {
return two + three
}
I cannot set any of the above values. two and three are let only (read only constants) and five is a computed property, so I can also only read that. I wont complicate the matter further but I would highly recommend giving this part of the documentation a read when you can.
Solution
You should have an outlet only:
#IBOutlet weak var imageView: UIImageView!
and then somewhere else in the code you can do things with it.
I think in your case it would be best on viewDidLoad. ViewDidLoad is called after the view and all of its outlets and properties have been set. So this is a safe place to start to use your outlets to further configure your view.
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
First of all, it's not good approach to override outlet's getter.
Secondly, technically you can override getter and setter of outlet.
In your case you defined getter, and forgot about setter. So you got get only variable.
private var _someView: UIImageView!
#IBOutlet weak var someView: UIImageView! {
get {
self.someView.contentMode = .scaleToFill
self.someView.clipsToBounds = true
return _someView
}
set {
_someView = newValue
}
}
You should use private variable to avoid recursion in getter.
Just try in this way and invoke this method inside viewDidLod()
func createRoundGreenImage(imageView : UIImageView, with image :
String) {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}

Line spacing doesn't work

I want to change line spacing for my textLabel in storyboard. I select text -> attributed and change paragraph line to 1. If I use textLabel in storyboard everything works fine. But If I connect textLabel in storyboard with my ViewController, this line spacing doesn't work. How to fix it?
My code:
struct Root : Decodable {
let questions : [Question]
}
struct Question : Decodable {
let number, text, rightAnswer : String
}
class ViewController: UIViewController, UITextFieldDelegate {
var questions = [Question]()
#IBOutlet var textLabel: UILabel!
func updateUI(with question: Question) {
title = question.number
textLabel.text = question.text
showAnswerLabel.text = question.rightAnswer
}
}
You can accomplished that by using a lazy var and some property observers. Configure the text styles the way you want in Interface Builder and use this code:
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
lazy var defaultAttributes = textLabel.attributedText!.attributes(at: 0, effectiveRange: nil)
var questionText: String {
get { return textLabel.text! }
set { textLabel.attributedText = NSAttributedString(string: newValue, attributes: defaultAttributes) }
}
override func viewDidLoad() {
super.viewDidLoad()
questionText = "What are the color of yellow cab taxi in New York City?"
}
}
Notce that we did not touch textLabel directly to change its text. We do so via the computed property questionText.
Result:

Value of type UIview has no member text

import UIKit
class ViewController: UIViewController {
#IBOutlet weak var userGuessTextField: UITextField!
#IBOutlet var resultLabel: UIView!
#IBAction func Guess(sender: AnyObject) {
let diceRoll = String(arc4random_uniform(6))
if diceRoll == userGuessTextField.text
{
resultLabel.text = "You are right"
}
else
{
resultLabel.text = "You are "
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Can anyone help me? Why I am getting this error as I am a beginner to iOS and learning this language.
please change...
#IBOutlet var resultLabel: UIView!
to
#IBOutlet var resultLabel: UILabel!
You are setting outlet like #IBOutlet var resultLabel: UIView!, It is type of UIView, so your resultLabel object is of UIView and it's not have property text
I think you should take outlet of UILabel instead of UIView.
So, drag a label in interface builder - ctrl + drag from it to class (in assistant editor) - and give outlet name.
Hope this will help :)
From Apple documents:
The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area.
... More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabel object draws a text string and a UIImageView object draws an image.
Remove the resultLabel from your storyboard/xib (delete the UIView and drag a new UILabel ) and from code change:
from:
#IBOutlet var resultLabel: UIView!
to
#IBOutlet var resultLabel: UILabel!
Then connect your new UILabel to the IBOutlet writed on code.

Changing font size by using UISlider

I can't get this to work, and this code is not inside viewDidLoad()
#IBOutlet weak var label: UILabel!
#IBOutlet weak var slider: UISlider!
#IBAction func slider(sender: UISlider) {
let senderValue = CGFloat(sender.value)
label?.font = UIFont(name: (label?.font.fontName)!, size:senderValue * 20)
label?.sizeToFit()
}
If you could help in any way that would be great.
The code does not have to be inside viewDidLoad.
Do you use autoLayout to position your view?
This should work:
#IBOutlet weak var label: UILabel!
#IBOutlet weak var slider: UISlider!
#IBAction func sliderAction(sender: AnyObject) {
print("Slider value \(slider.value)")
self.label.font = UIFont.systemFontOfSize(CGFloat(slider.value * 20.0))
}
Check if:
UIFont object is initialised correctly.
Remove label?.sizeToFit. Normally, the label text is drawn with the font you specify in the font property.
Constraints are correctly set on the UILabel object.

Resources