Strange behaviour with monospaced font in UILabel when changing text - ios

I have a strange issue when using monospacedDigitSystemFont(ofSize:weight:)
I have one UISlider and one UILabel in my UIViewController. The Label is showing the current value of the slider + some description text. When changing the slider's value, the text of myLabel is shaking left and right a bit. I would expect the myLabel's text to not shake left and right, since I am using monospacedDigitSystemFont(ofSize:weight:).
This is my code:
import UIKit
class ExampleViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet weak var mySlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.font = UIFont.monospacedDigitSystemFont(ofSize: 15, weight: .bold)
}
#IBAction func sliderChanged(_ sender: UISlider) {
myLabel.text = String(format: "%.5f is the actual Value of the Slider", sender.value)
}
}
GIF about the jiggle:
Any suggestions? Am I missing something?

This is a horrible bug and it's caused by the monospaced font being bold. Setting the weight to regular solves this problem.
myLabel.font = UIFont.monospacedDigitSystemFont(ofSize: 15, weight: .regular)
If you still want to use a bold font, consider using a non-standard monospaced font.

Related

How to Set fonts of OutletCollection of Label?

I have taken outlet collection of labels and these labels will have same fonts and I want to set the font for all labels in one line.
It will go like this:
labels.forEach { $0.font = UIFont.systemFont(ofSize: 43) }
Here is how,
class ViewController: UIViewController {
#IBOutlet var labels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
labels.forEach { (label) in
label.font = UIFont.systemFont(ofSize: 12, weight: .semibold)
//give the font as per your requirement...
}
}
}
Just get all the labels you want inside an array of Labels
let myLabels: [UILabel] = [label1,label2,label3]
then make a Loop to set the font of each one of the labels..
for text in myLabels {
text.font = UIFont(name: "Kefa-Regular", size: 10)
//The 'name' is the name of the font you want, it's also could be a custom one.
}
if it's an Outlet Collection you can skip the Array part of the code.

UILabel cannot show its all letters

UILabel cannot show its all letters.
My app is like now my app
PhotoController (it is for this page)is
import Foundation
import MobileCoreServices
import UIKit
class PhotoController:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate{
#IBOutlet weak var label: UILabel!
#IBOutlet weak var myImageView: UIImageView!
private var imagePicker:UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Tap the PhotoSelect or Camera to upload a picture"
//myImageUploadRequest()
}
So,all letter of
label.text = "Tap the PhotoSelect or Camera to upload a picture"
is not shown in my app.
What should I do to fix this?
label.numberOfLines = 0
Thats all you need :)
EDIT:
Setting line break modes to the label will help you get further improved O/P
label.lineBreakMode = .byWordWrapping //choose whichever matches ur requirement
label.numberOfLines = 0
Either make your label multi line or
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
from storyboard
Constraints :
Give constant label height with relationship >=
Property
set number of lines = 0

how to print uiview in label (swift3)

I am trying to print a uiview in a label. I don't know if I should put accessibilityIdentifier or not. When I did put accessiblitiyIdentifier in my whole app loaded but was all black.
#IBOutlet var pink: UIView!
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = pink
}
AccessibilityIdentifier can use for UI Automation scripts,
label.text = pink wont be compile
you can use label.addSubview(pink)
label.text is of type String and pink is of type UIView!, that shouldn't even compile?

Incorrect capitalization in UILabel

The code I am using:
#IBOutlet weak var tlabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tlabel.font = UIFont(name: "Anxiety", size: 40)
tlabel.numberOfLines = 0
tlabel.text = "Test"
}
The text "Test" gets capitalized ("TEST" appears on the screen). How can I avoid this?
A quick google seems to show that the font Anxiety does not have any non capital letters in the font. So if you want to use non capital letters you have to find another font.
http://www.1001freefonts.com/anxiety.font

How to change text UILabel in swift code?

I'm new to swift and I am trying to understand UILabel text property
Based on what I have read, my understanding is that I can replace text with any String . So I did
The text property is "read-only" , is there a way I can change text label by hardcoding in swift ?
I am not in front of my computer to verify this, but I don't think you need to mark it as "self"
Just Food.text = "Burger"
Additionally. It is common for you to put variables with the first letter as lower case with each other words first letter being upper case
For example.
var firstWord = "Traditionally the best way to code"
Capital first letter first word is traditionally used for classes.
Example
Class NewClass = "Blah Blah"
You cannot set a value like this in the declaration section of the class.
Since you're working with a UIViewController, you'll want to go to the viewDidLoad() method and put self.Food.text = "Burger" there.
For example:
import UIKit
class TestViewController: UIViewController {
#IBOutlet weak var Food: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.Food.text = "Burger"
}
}
The viewDidLoad() method is called by the system when (coincidentally enough) the view is first loaded. After the view has loaded, you are free to make any edits to any of the objects on the screen. viewDidLoad will be where you will want to perform the majority of the setting up of your view.
100% programatically would be like this. This both creates the label and the string.
override func viewDidLoad()
{
super.viewDidLoad()
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "Burger"
self.view.addSubview(label)
}

Resources