0

How to fix the textlabel of cell have different spacing bettween text and image how to fix this this table is creating by coding

class NewMessageCell: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}



override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}

override func layoutSubviews() {
    super.layoutSubviews()
    self.imageView?.frame = CGRect(x: 5, y: 10, width: 70, height: 70)
    self.imageView?.clipsToBounds = true
    self.textLabel?.highlightedTextColor = UIColor.blue

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

Table Image

1 Answers1

0
self.imageView?.frame = CGRect(x: 5, y: 10, width: 70, height: 70)

If you set the bounds of UIImageView then it will get messed up. The UIImageView will automatically fill the size of the cell so if you are programmatically creating the view the you have control over the whole view's size.

If you need to set all the images to a new size so that they work, use this function (I updated it for iOS 10 from this question:

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    return UIGraphicsImageRenderer(size: newSize).image { ctx in
        image.draw(in: rect)
    }
}