I cannot get my label's background property to properly expand/grow with the text as it is being inputted into the label.
I've made a UITableView with a custom cell. This custom cell's file is a .xib file (called StoryCell.xib). The .xib file is associated with a .swift file that I can enter code in. In this .swift file (it is called StoryCell.swift), I have a typing animation function where whatever string goes into this label, it will be typed out character by character until the full text of that string variable is fully typed out over a period of time. With this label, I have a background property set. (for effects).
My problem is that when the characters and words are being typed into the label with the inputted string variable, the background property does not properly expand with each line of the text being typed out. Playing around with different code and enabling different properties and constraints, my label's background will either be pre-determined (meaning that the background property will automatically be set to certain dimensions [a certain box] to fit the entirety of the text once it's fully typed out) or that my background property just stays as one single line, cutting off text after that line moves onto another line to write more text.
I cannot for the life of me figure out what to do. If anyone has any idea on what I can do or resources I can look into, I'll be eternally grateful.
TL;DR I want my UILabel's background property in my custom cell (in a UITableView) to expand line by line with my text being typed out by my typing function, but I can't achieve this effect. How to get?
My StoryCell.swift's code for reference if you need:
import UIKit
class StoryCell: UITableViewCell {
var typingTimer: Timer? // Add typingTimer property
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// Add startTypingAnimation method
func startTypingAnimation(withText text: String) {
label.text = "" // Clear existing text
var charIndex = 0
// Create a timer to simulate typing animation
typingTimer?.invalidate() // this code line here is to make sure that no previous timers are running
typingTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { timer in
if charIndex < text.count {
let index = text.index(text.startIndex, offsetBy: charIndex) // The text.index(_, offsetBy:) method is used to obtain the index of a character in the string text. It takes two arguments: the starting index and an offset. The offset specifies the number of positions to move from the starting index.
let nextChar = String(text[index]) // the character at the position specified by the offset value
self.label.text?.append(nextChar)
charIndex += 1
} else {
timer.invalidate()
}
}
}
}
If you must know, the label's text comes from another .swift file which is why you won't see any text in this file.




I'm not sure why exactly your constraints didn't work. I remember doing something similar before, and the cell automatically resizes as expected.
In any case, the cell does automatically resize if you use the
UIListContentConfigurationAPI, which existed since iOS 14. This allows you customise table view cells in various ways, without manually adding the subviews yourself. Of course, it is less flexible than hand-crafting your own cells, but the customisation it allows is still quite a lot. There are also related classes likeUIBackgroundConfiguration.Here is
startTypingAnimation, but changed to useUIListContentConfiguration: