vendredi 8 mai 2015

UILabel flicker when changing font dynamically on UITableViewCell

tableView.estimatedRowHeight = 90.0
tableView.rowHeight = UITableViewAutomaticDimension
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
    let cell = self.tableView.dequeueReusableCellWithIdentifier("DetailCell") as DetailCell

    var detail : Detail
    detail = Details[indexPath.row]

    var paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 15
    paragraphStyle.alignment = NSTextAlignment.Right

    var attrString = NSMutableAttributedString(string: detail.detailText)
    attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

    var fontName = cell.myLabel.font.fontName

    cell.myLabel.font = UIFont(name:fontName, size: pointSize)
    cell.myLabel.attributedText = attrString


    return cell
}

pointSize changes as the user pinches (used UIPinchRecognizer) the default pointSize of myLabel is 17. The code works perfectly when pinched to increase or decrease the pointSize. But while scrolling first the default size is applied and then increased or decreased size due to this the cells are giving a flicker effect while scrolling.

How do I avoid this? Is there any sample example already done for UITableViewCells

Aucun commentaire:

Enregistrer un commentaire