Resize the width of the UILabel exactly to the length of text in the label

how to calculate uilabel width based on text length swift
uilabel width to fit text swift
adjust uilabel height depending on the text swift 4
swift resize text to fit label
uilabel dynamic width programmatically
uilabel sizetofit
uilabel font size adjust to fit programmatically swift
uilabel fit text width

I am trying to resize the width of the UILabel exactly to the length of text in the label. I also want to print the current size of the label but not sure how, in this most recent version of Swift.

` func createTxt (fontColor: UIColor , xco: CGFloat, yco: CGFloat, widthLength: CGFloat, heightLength: CGFloat, phrase : String, fontName: String, fontSize: CGFloat) {

    let label = UILabel(frame: CGRect(x: xco, y: yco, width: widthLength, height: heightLength))
    label.textColor = fontColor
    label.font = UIFont(name: label.font.fontName, size: fontSize)
    label.textAlignment = .left
    label.text = phrase
    label.backgroundColor = .red
    self.view.addSubview(label)
}
`

If you need to find the size of the label and set the frame manually you can use the sizeThatFits(_:) method.

Example:

let label = UILabel()
label.text = someLongText
label.frame = CGFrame(point: point, size: label.sizeThatFits(CGSize(
    width: CGFloat.infinity,
    height: yourStaticHeight
)))

Otherwise you could just use the sizeToFit() method, but you would need to set your layout constraints well for that to work.

Objective c uilabel width based on text length, You might want to use [yourLabel sizeToFit]; or [yourLabel sizeThatFits: <#your target size#>];. sizeToFit resizes your label within it's current frame.size according � If you need to find the size of the label and set the frame manually you can use the sizeThatFits(_:) method. Example: let label = UILabel() label.text = someLongText label.frame = CGFrame(point: point, size: label.sizeThatFits(CGSize( width: CGFloat.infinity, height: yourStaticHeight )))

populate the label with its text and then:

label.sizeToFit()

You can then access its .frame property to get its .size and .height

iOS, Sometimes we have to resize a UILabel based on dynamic content where the text length is unknown. In this example, width of the UILabel is fixed at 280 points� Let's fill the UILabel with unreasonably long text to see how that will work and run the project and see how that looks in the simulator. With text of reasonable size, you will from screen shot below that UILabel and UIView have grown vertically to accommodate text keeping width fixed.

If you set UILabel with frame, you should use:

label.sizeToFit()

If you set UILabel in storyboard, only set its top, bottom and Horizontal Constraint.

UILabel / Auto-size label to fit text / Essential iOS, Don't add constraints for the width and height. Labels have an intrinsic size based on their text content. No need to set sizeToFit when using auto layout. Ok the problem: I have a UILabel which gets text dynamically assigned to it. If I resize the label to cover the entire view controller the text is stuck in the middle. I found a bunch of solutions which say to add the following code: detailsLabel.numberOfLines = 0; detailsLabel.sizeToFit() However this doesn't do anything.

Adjust UILabel to change it's frame according to it's content � GitHub, It's because I offer the ability to my users to resize their label size but I don't want the text to overstep the frame size. So I am trying to evaluate the text min height� I have a UILabel which receives text dynamically, and the length of that text differs. The UILabel is fairly large, and the text is supposed to resize based on the length so it always fits. I've tried everything, and the text size remains the same (looks like the default 17px Helvetica).

Auto Layout Guide: Views with Intrinsic Content Size, In this example, the label's width is based on the size of its text property, and the text field expands and� If you set type with vw (viewport width) units, you can find an exact number where the text pretty closely fits the container and doesn’t break as you resize. I’d call this a magic number. In this case, font-size: 25.5vw; works down to a 320px viewport, but still will break much lower than that.

How do I size a UILabel to fit around the text I give it?, If its a little bit of text, it needs to fit the width and height of that, a long and only found answers for the opposite: How to shrink font size to fit a fixed sized label. While recommend setting the "correct" paper size in the article, adjusting the paper size to an "incorrect" size might achieve the end you're looking for. USPS doesn't require a specific label size, so making any changes to your printer defaults should not cause any delivery errors. We simply offer 4x6 labels (as well as 4x5 and 4x8) as they

Comments
  • @bain good to hear - can you mark the answer as accepted please.