Changing font size by using UISlider - ios

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.

Related

How add gesture into the tableviewcell?

everyone!
I am trying to add gesture to a UIImageView into the TableViewCell.
When click on the ImageView - the color changes.
class StocksCellView: UITableViewCell {
#IBOutlet weak var logoImageView: UIImageView!
#IBOutlet weak var tickerLabel: UILabel!
#IBOutlet weak var favouriteImageView: UIImageView!{ didSet {
let panGesture = UITapGestureRecognizer(target: self,
action: #selector(gestureAction))
favouriteImageView.addGestureRecognizer(panGesture)
}
}
#IBOutlet weak var companyNameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var deltaLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
#objc func gestureAction() {
favouriteImageView.tintColor = UIColor.yellow
}
}
But nothing works.
If i change favouriteImageView.addGestureRecognizer(panGesture)
on addGestureRecognizer(panGesture)
Then, by clicking on the cell, the color will change.
But I want by clicking on the picture.
enter image description here
First of all you must check and change like below:
favouriteImageView.isUserInteractionEnabled = true

Hierarchy Problem with presenting Viewcontrollers, can someone help me?

[![enter image description here][2]][2]i want to programm an App with two ViewControllers
The first one performs a segue to the second VC.
The Second VC has a Scrollview with different Views.
I did everything with Autolayout not with code
When i run the Application i get always the error "whose view is not in the window hierarchy!" and can't scroll to the bottom. I tried to find some solutions but nothing helped.
Hope someone can help me with the problem.
my first VC runs a normal performSegue("identifier",sender )
my code for the second VC is
import UIKit
class FragenViewController: UIViewController {
var fragen = [Question]()
var highscore = 0
var questionNumber = 0
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerButton1: UIButton!
#IBOutlet weak var answerButton2: UIButton!
#IBOutlet weak var answerButton3: UIButton!
#IBOutlet weak var answerButton4: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var currentQuestionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func answerButton_Tapped(_ sender: UIButton) {
}
}
the rest is with Main Storyboard Drag and drop
The Code from the first VC is
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var highscoreLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Layout
startButton.backgroundColor = UIColor.systemFill
startButton.layer.cornerRadius = 20
startButton.titleLabel?.font = UIFont(name: "Arial", size: 20)
startButton.layer.borderWidth = 2
startButton.tintColor = UIColor.systemBlue
highscoreLabel.font = UIFont(name: "Arial", size: 18)
}
#IBAction func start_Button(_ sender: Any) {
performSegue(withIdentifier: "startGame", sender: sender)
}
}

Get adjusted font size of label

I have a header label which I am adjusting the font size of, to fit to the size of the label. I would like to store the value of that font size so I can set the font size of some buttons to it.
I have tried to just store the pointSize of the label, but that just gives me the unadjusted value, not the value that is actually in effect.
The reason for this is, that I want the size of all the text in labels and buttons to look good on different sized iPad screens. But I am struggling with keeping the size the same, across multiple elements.
class ViewController: UIViewController {
#IBOutlet weak var essenceButton: UIButton!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var tribesButton: UIButton!
#IBOutlet weak var creaturesButton: UIButton!
#IBOutlet weak var monstersButton: UIButton!
#IBOutlet weak var musesButton: UIButton!
#IBOutlet weak var newsletterButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.titleLabel.adjustsFontSizeToFitWidth = true
let fontsize = self.titleLabel.font.pointSize
self.essenceButton.titleLabel?.font = essenceButton.titleLabel?.font.withSize(fontsize)
self.tribesButton.titleLabel?.font = tribesButton.titleLabel?.font.withSize(fontsize)
}
}
At viewDidLoad(), the view and its subviews (including the label) have not yet been laid out for the device's screen, so the label's font size has not changed.
You should be able to do this in viewWillLayoutSubviews():
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
view.layoutIfNeeded()
// Code to adjust font of the other UI items here
}

Not woking UIView ishidden after change UILabel text value in swift

I want to change uilabel value and show uiview that has hide like a popup windows.
When I touch a button, that code print "setPopupView 0".
but doesn't show settingView.
and I touch a button again.
print "setPopupView 0" and show settingView.
so When "settingLabelValueUnit text" has changed not show settingView,
but when "settingLabelValueUnit text" has not changed show settingView.
(It means "settingLabelValue text" is same as input value)
I don't know why.
Anyone can help me?
Thank you
here is my swift code.
class ViewController: UIViewController {
#IBOutlet weak var workoutScrollView: UIScrollView!
#IBOutlet weak var settingView: UIView!
#IBOutlet weak var settingLabelValueUnit: UILabel!
#IBOutlet weak var imgSettingBGcal: UIImageView!
#IBOutlet weak var imgSettingBGdis: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imgSettingBGcal.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(calSettingClick)))
self.imgSettingBGdis.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(disSettingClick)))
}
func calSettingClick(sender: UITapGestureRecognizer) {
setPopupView(0)
}
func disSettingClick(sender: UITapGestureRecognizer) {
setPopupView(1)
}
func showPopup (_ settype:Int) {
switch settype {
case 0:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set1"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
case 1:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set2"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
}
}
}

Why viewWithTag is returning nil

activite1Label as the tag 1
class StatsViewController: UIViewController {
#IBOutlet weak var activite1Label: UILabel!
#IBOutlet weak var activite2Label: UILabel!
#IBOutlet weak var activite3Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
activite1Label.text = activite[0]
activite2Label.text = activite[1]
activite3Label.text = activite[2]
miseAjourTotal()
}
func miseAjourTotal() {
let leLabel = view.viewWithTag(1) as! UILabel
print("leLabel: \(leLabel.text)")
}
}
Nothing in your code tells that the label has the tag 1. You should go to your storyboard and check if the label does have tag 1 or set the tag programmatically

Resources