I am working on a quick application. I'm currently working on a collection of tabular views with custom cells, see screenshot . However, right now I have a text set, so the title has exactly 2 lines and the summary has exactly 3 lines. By doing this, the text is sometimes truncated. Now I want to set the priority for the text in the title, so if the title is truncated when it lasts 2 lines, I expand it to 3 lines and make a summary of only two lines. I tried to do this with auto-layout, but failed. Now I tried the following approach, in accordance with this and this , but the function below also did not appear to determine exactly whether the text is truncated.
func isTruncated(label:UILabel) -> Bool { let context = NSStringDrawingContext() let text : NSAttributedString = NSAttributedString(string: label.text!, attributes: [NSFontAttributeName : label.font]) let labelSize : CGSize = CGSize(width: label.frame.width, height: CGFloat.max) let options : NSStringDrawingOptions = unsafeBitCast(NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue | NSStringDrawingOptions.UsesFontLeading.rawValue, NSStringDrawingOptions.self) let labelRect : CGRect = text.boundingRectWithSize(labelSize, options: options, context: context) if Float(labelRect.height/label.font.lineHeight) > Float(label.numberOfLines) { return true } else { return false } }
Can anyone help? How can I change my function to make this work? Or should it work with various auto-layout limitations and how? Thank you very much!
EDIT: this is my current code. Some automatic layout is done, this is a storyboard, however, changing the automatic layout is done in code. import UIKit
class FeedTableViewCell: UITableViewCell { var thumbnailImage = UIImageView() @IBOutlet var titleText: UILabel! @IBOutlet var summaryText: UILabel! @IBOutlet var sourceAndDateText: UILabel! var imgTitleConst = NSLayoutConstraint() var imgSummaryConst = NSLayoutConstraint() var imgDetailConst = NSLayoutConstraint() var titleConst = NSLayoutConstraint() var summaryConst = NSLayoutConstraint() var detailConst = NSLayoutConstraint() var titleHeightConst = NSLayoutConstraint() var summaryHeightConst = NSLayoutConstraint() var imageRemoved = false var titleConstAdd = false override func awakeFromNib() { super.awakeFromNib() thumbnailImage.clipsToBounds = true summaryText.clipsToBounds = true titleText.clipsToBounds = true sourceAndDateText.clipsToBounds = true addImage() } func removeImage() { if let viewToRemove = self.viewWithTag(123) { imageRemoved = true viewToRemove.removeFromSuperview() self.contentView.removeConstraints([imgTitleConst, imgSummaryConst, imgDetailConst]) titleConst = NSLayoutConstraint(item: self.titleText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 14) summaryConst = NSLayoutConstraint(item: summaryText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 14) detailConst = NSLayoutConstraint(item: sourceAndDateText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 14) self.contentView.addConstraints([titleConst, detailConst, summaryConst]) setNumberOfLines() self.contentView.layoutSubviews() } } func addImage() { thumbnailImage.tag = 123 thumbnailImage.image = UIImage(named: "placeholder") thumbnailImage.frame = CGRectMake(14, 12, 100, 100) thumbnailImage.contentMode = UIViewContentMode.ScaleAspectFill thumbnailImage.clipsToBounds = true self.contentView.addSubview(thumbnailImage) if imageRemoved { self.contentView.removeConstraints([titleConst, summaryConst, detailConst]) } var widthConst = NSLayoutConstraint(item: thumbnailImage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100) var heightConst = NSLayoutConstraint(item: thumbnailImage, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100) var leftConst = NSLayoutConstraint(item: thumbnailImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 14) var topConst = NSLayoutConstraint(item: thumbnailImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 12) imgTitleConst = NSLayoutConstraint(item: self.titleText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.thumbnailImage, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8) imgSummaryConst = NSLayoutConstraint(item: summaryText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.thumbnailImage, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8) imgDetailConst = NSLayoutConstraint(item: sourceAndDateText, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.thumbnailImage, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8) self.contentView.addConstraints([widthConst, heightConst, leftConst, topConst, imgTitleConst, imgSummaryConst, imgDetailConst]) setNumberOfLines() self.contentView.layoutSubviews() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated)