UILabel: adjustsFontSizeToFitWidth doesn't work - ios

I want to make a string to fit the size of label. The code below doesn't change anything. What am I doing wrong?
nameLabel.font = UIFont.systemFont(ofSize: 30)
nameLabel.adjustsFontSizeToFitWidth = true
nameLabel.text = "fjggfjghggiuhgughuohgoihiohiohiughiugyiugyu8ftufuyguoy80houhuiy78rt6drtyreruti"
nameLabel.numberOfLines = 1
nameLabel.minimumScaleFactor = 0.5

UIKit needs a hint about how many lines of text you want, or it doesn't know how much to scale down. If you want the whole text to fit into the label on one line, you also need: nameLabel.numberOfLines = 1.
Working playground example:
import Foundation
import UIKit
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.text = "Some very long text goes here"

Related

See image below, why is it the combined height of two labels (brown) different with the height of the label (red)?

I just cut the original string \nfoo\n\nbar\n\nfoo\n\nbar\n into two different strings \nfoo\n\nbar\n and \nfoo\n\nbar\n. The cut strings will be set into its dedicated label. I was expecting that the combined height of the two labels would just be the same with a label that has the original string. Can you guide me into something that explains this? Is there a way to make the combined two labels the same with the one label without adding/deleting a character from the cut strings?
Playground
import PlaygroundSupport
import Foundation
import UIKit
PlaygroundPage.current.needsIndefiniteExecution = true
let viewWidth: CGFloat = 100.0
func createLabel(_ text: String, _ backgroundColor: UIColor = .white) -> UILabel {
let label = UILabel()
label.text = text
label.numberOfLines = 0
label.backgroundColor = backgroundColor
label.textColor = .black
label.sizeToFit()
label.frame.size.width = viewWidth
return label
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: viewWidth, height: 1000))
view.backgroundColor = .white
let label1 = createLabel("\nfoo\n\nbar\n", .green)
let label2 = createLabel("\nfoo\n\nbar\n", .blue)
let label3 = createLabel("\nfoo\n\nbar\n\nfoo\n\nbar\n", .red)
let label4 = createLabel("\nfoo\n\nbar\n", .brown)
let label5 = createLabel("\nfoo\n\nbar\n", .brown)
label2.frame.origin.y = label1.frame.maxY
label3.frame.origin.y = label2.frame.maxY
label4.frame.origin.y = label3.frame.maxY
label5.frame.origin.y = label4.frame.maxY
view.addSubview(label1)
view.addSubview(label2)
view.addSubview(label3)
view.addSubview(label4)
view.addSubview(label5)
PlaygroundPage.current.liveView = view
/*
\nfoo\n\nbar\n
\nfoo\n\nbar\n
*/
when you combine the label4 and label5 that way, you have three new line,
the label4 should be let label4 = createLabel("\nfoo\n\nbar", .brown)
remove \n at the end.

Label not displaying full text swift

I set up label programmatically and want to change the text of the label once a button is clicked. However, the label just displays part of the text. For example, I want to change the text from "press to start a trip" to "searching for GPS signal..", only "searching for" displayed even without "..." to indicate that there are more texts.
let startTravelBtn = UIButton(frame: CGRect(x: 40, y: 50, width: 50,
height: 30))
let startTipLabel = UILabel(frame: CGRect(x: 100, y: 50, width: 200, height: 30))
Solution
yourLabel.text = "Lorem Ipsum is simply dummy text of the printing"
yourLabel.sizeToFit()
OR
Set Lines on label to 0
public let descriptionLabel: UILabel = {
let label = PaddingLabel(withInsets: 10, 10, 10, 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont(name: "Helvetica Neue Light", size: 17)
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0
label.sizeToFit()
(label.lineBreakMode = .byWordWrapping) - doesn't also work
I tried many methods As a result, only this method
(label.adjustsFontSizeToFitWidth = true) helps to avoid hiding the text.
You can do this by setting numberOfLines to 0
startTipLabel.text = "longer text what you want"
startTipLabel.numberOfLines = 0
startTipLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping

See nothing after adding label to scrollview

I am trying to add label to scrollview in swift but it doesn't works for me, here my code:
let messageLbl: UILabel = UILabel()
messageLbl.frame = CGRect(x: 0, y: 0, width: self.resultsScrollView.frame.size.width-60, height: CGFloat.greatestFiniteMagnitude)
messageLbl.backgroundColor = UIColor.groupTableViewBackground
messageLbl.lineBreakMode = NSLineBreakMode.byWordWrapping
messageLbl.textAlignment = NSTextAlignment.left
messageLbl.numberOfLines = 0
messageLbl.font = UIFont(name: "Helvetica Neuse", size: 17)
messageLbl.textColor = UIColor.black
messageLbl.text = "myTextGoesHere"
messageLbl.sizeToFit()
messageLbl.layer.zPosition = 20
messageLbl.frame.origin.x = (self.resultsScrollView.frame.size.width - self.messageX) - messageLbl.frame.size.width
messageLbl.frame.origin.y = self.messageY
self.resultsScrollView.addSubview(messageLbl)
But I see nothing when I run my app:
If you "see nothing when [you] run [your] app" that doesn't mean it doesn't exist in the hierarchy of your views. Debug your code by printing messageLbl.frame.origin.x and check if you get a value bigger than the width of your screen.
Adjusting the code in this line will do the trick:
messageLbl.frame.origin.x = (self.resultsScrollView.frame.size.width - self.messageX) - messageLbl.frame.size.width

UILabel and UITextView line breaks don't match

I have a UILabel and a UITextView, where my intention is for them both to display the same text, and for the appearance to be the same. I'm having some issue with that right now. I've set up a demonstration in a Swift Playground:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 100, y: 100, width: 247, height: 39)
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 16)
label.text = "I’m on my way back to London today"
label.textColor = .black
label.backgroundColor = .red
label.lineBreakMode = .byWordWrapping
view.addSubview(label)
let textView = UITextView()
textView.frame = CGRect(x: 100, y: 200, width: 247, height: 39)
textView.font = label.font
textView.text = label.text
textView.textColor = label.textColor
textView.backgroundColor = label.backgroundColor
textView.textContainer.lineBreakMode = label.lineBreakMode
textView.textContainerInset = UIEdgeInsets.zero
textView.textContainer.lineFragmentPadding = 0
view.addSubview(textView)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
As you can see, they're both setup in the same way, but the linebreak occurs in a different place on the UITextView. I've set the lineBreakMode to the same value for both. I've also removed the textContainerInsets and lineFragmentPadding on the UITextView, so the text is positioned with the same padding within both views.
It was a change to UILabel in IOS 11 to fix orphaned words. No way to shut it off although I wish there was. The only way to do it would be to use a non editable/nonscrollable textview or a CATextLayer. To get the sizing attributes of a UILabel I am afraid you have to do that manually.
See this as the problem was similar. Although not in that answer I did find that UITextView wraps the old way. I am using a UITextView now and I manually adjust the font size in textDidChange. I hold the original font so I can always resize.

Why setting titleView lowers header text?

I'm trying to dynamically set my navigation bar's text so that the header text always fits. I'm acccomplishing that like this:
// Pet's Day text "Joy's Day"
if let range = currentPet.range(of: "_") {
let petsName = currentPet[range.upperBound..<currentPet.endIndex]
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "\(petsName)'s Day"
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
However, as seen by this picture, this lowers the header text below its natural height:
The navigation bar on the left is from one of my app's other views, while the one on the right is the one I'm setting.
Both of these navigation bars are navigation bars that I've dragged in and made the prompt equal to an empty string to increase their height:
Can anybody please help me implement my code above so that it doesn't drop down the header text?
**Edit: Here are screenshots from Xcode's debug hierarchy:
This is the normal navigation bar:
This is the one I'm setting:
You need to set the baseline, by default is Align Baseline, you need to change to Align Centers
Code example:
class TestViewController: UIViewController {
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Change the height of the navbar
self.navBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 68)
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "Log Events" // Change to Joy's Day and check the result
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.baselineAdjustment = .alignBaselines
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
}

Resources