resize label font during resize selected cell with animation - swift

Hi All,


please how can I resize label font during resize selected cell with animation in swift. I have set constraints for label and this is resized correctly with animation during resize selected cell, but not his label font. For label have set also content mode : Scale to fill and autoshrink : minimum font size.

Thank You

It would be easier if you had posted the code for the tableView(cellForRowAt:)


But, I assume you have a UILabel outlet for the text in the cell template:


@IBOutlet var valueLabel: UILabel!


in tableView(cellForRowAt:), where you build the cell to return add:

let newSize = 18 // compute the size you want, propotional to cell.height
let attributesForSize = [NSAttributedStringKey.font : NSFont.systemFont(ofSize: newSize)
cell!.valueLabet.attributext = NSAttributedText(string"the text to display", attributes: attributesForSize)

In my opinion, here is misunderstanding. For resize selected cell I use method below. And when I resize cell with this method, then I dont go to method tableView(cellForRowAt:) already.


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

{

if selectedCellIndexPath == indexPath {

return selectedCellHeight

}

return unselectedCellHeight

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "sum cell") as UITableViewCell?

if cell == nil {

cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "sum cell")

}


let label = cell?.contentView.viewWithTag(1) as! UILabel

label.text = String(sums[indexPath.row])


return cell!

}

I did not understand that you resized when selecting cell.


Can you post your implementation of:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


You could probably put the code here


let largeSize = 18 // compute the size you want, propotional to cell.height
let normalSize = 12
if selectedCellIndexPath == indexPath {
     let attributesForSize = [NSAttributedStringKey.font : NSFont.systemFont(ofSize: largeSize)
     cell!.valueLabel.attributext = NSAttributedText(string"the text to display", attributes: attributesForSize)
} else {
     let attributesForSize = [NSAttributedStringKey.font : NSFont.systemFont(ofSize: normalSize)
     cell!.valueLabel.attributext = NSAttributedText(string"the text to display", attributes: attributesForSize)
}

You can see picture from my question on beginning. Here is method for select cell:

All this methods which I sent you, make resize selected cell with animation - and also label boundary, but not label font inside and its animation.


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath.row >= 2)
{
let indexPath = IndexPath(row: indexPath.row - 2 , section: 0)
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
}
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
selectedCellIndexPath = nil
} else {
selectedCellIndexPath = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()

if selectedCellIndexPath != nil {
tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}
if let currentCell = tableView.cellForRow(at: indexPath) {
let label = currentCell.contentView.viewWithTag(1) as! UILabel
label.backgroundColor = UIColor.orange
}
}

Thank You

So, you could try to complement like this :


       let largeSize = 18 // compute the size you want, propotional to cell.height
       let normalSize = 12
        if let currentCell = tableView.cellForRow(at: indexPath) {
            let label = currentCell.contentView.viewWithTag(1) as! UILabel
            label.backgroundColor = UIColor.orange
            let attributesForSize = [NSAttributedStringKey.font : NSFont.systemFont(ofSize: normalSize)
            label.attributext = NSAttributedText(string: label.text, attributes: attributesForSize)
        }

yes, the font is resized, but without animation - only change size in one time. But I want to animate resizing font same as label size is resizing.

Thank You

Never did this, so I searched and found this objC code, if that bmay help:

h ttps://stackoverflow.com/questions/14494566/animating-uilabel-font-size-change


But unless you animate an image of the text, the font size change will not be smooth

resize label font during resize selected cell with animation - swift
 
 
Q