diff --git a/ObjC/AutomaticHeightTagTableViewCell/Models/TagGroups.json b/ObjC/AutomaticHeightTagTableViewCell/Models/TagGroups.json index b845607..0fb40ff 100644 --- a/ObjC/AutomaticHeightTagTableViewCell/Models/TagGroups.json +++ b/ObjC/AutomaticHeightTagTableViewCell/Models/TagGroups.json @@ -23,7 +23,13 @@ "ENABLED": true, "CATEGORY": "Pinterest", "TITLE": "Boards" - }], + }, { + "URL": "http:\/\/pinterest.com", + "COLOR": "0xFF8F8F", + "ENABLED": true, + "CATEGORY": "Pinterest", + "TITLE": "The tag is chopped if it's wider than the width of the display." + }], [{ "URL": "http:\/\/itunes.com", "COLOR": "0xA08FFF", diff --git a/ObjC/AutomaticHeightTagTableViewCell/ViewController.m b/ObjC/AutomaticHeightTagTableViewCell/ViewController.m index c7b561e..57c8915 100644 --- a/ObjC/AutomaticHeightTagTableViewCell/ViewController.m +++ b/ObjC/AutomaticHeightTagTableViewCell/ViewController.m @@ -99,4 +99,15 @@ - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view v.textLabel.textColor = [UIColor darkGrayColor]; } +#pragma mark - UIViewControllerTransitionCoordinator + +- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id )coordinator +{ + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; + + [coordinator animateAlongsideTransition:^(id context) { + [self.tableView reloadData]; + } completion:^(id context) {}]; +} + @end diff --git a/ObjC/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.m b/ObjC/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.m index 3c10e16..9cbe5b8 100644 --- a/ObjC/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.m +++ b/ObjC/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.m @@ -9,6 +9,8 @@ #import "AHTagsLabel.h" #import "AHTagView.h" +#define MAX_WIDTH (float)([UIScreen mainScreen].bounds.size.width - 15) + @implementation AHTagsLabel - (instancetype)initWithFrame:(CGRect)frame { @@ -92,11 +94,16 @@ - (void)setTags:(NSArray *)tags { AHTagView *view = [AHTagView new]; view.label.attributedText = [AHTagsLabel attributedString:title]; view.label.backgroundColor = color; - + CGSize size = [view systemLayoutSizeFittingSize:view.frame.size withHorizontalFittingPriority:UILayoutPriorityFittingSizeLevel verticalFittingPriority:UILayoutPriorityFittingSizeLevel]; - view.frame = CGRectMake(0, 0, size.width + 20, size.height); + view.frame = CGRectMake(0, 0, MIN(size.width + 20, MAX_WIDTH), size.height); + if (size.width + 20 > MAX_WIDTH) { + title = [title substringToIndex:(NSUInteger)(title.length * MAX_WIDTH/(size.width + 20)) - 2]; + title = [NSString stringWithFormat:@"%@...", title]; + view.label.attributedText = [AHTagsLabel attributedString:title]; + } [cell.contentView addSubview:view]; UIImage *image = view.image; @@ -120,6 +127,7 @@ + (NSAttributedString *)attributedString:(NSString *)string { paragraphStyle.firstLineHeadIndent = 10.0f; paragraphStyle.headIndent = 10.0f; paragraphStyle.tailIndent = 10.0f; + NSDictionary *attributes = @{ NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0] diff --git a/Swift/AutomaticHeightTagTableViewCell/Models/TagGroups.json b/Swift/AutomaticHeightTagTableViewCell/Models/TagGroups.json index b845607..9bfa2b9 100644 --- a/Swift/AutomaticHeightTagTableViewCell/Models/TagGroups.json +++ b/Swift/AutomaticHeightTagTableViewCell/Models/TagGroups.json @@ -23,7 +23,13 @@ "ENABLED": true, "CATEGORY": "Pinterest", "TITLE": "Boards" - }], + }, { + "URL": "http:\/\/pinterest.com", + "COLOR": "0xFF8F8F", + "ENABLED": true, + "CATEGORY": "Pinterest", + "TITLE": "The tag is chopped if it's wider than the width of the display." + }], [{ "URL": "http:\/\/itunes.com", "COLOR": "0xA08FFF", diff --git a/Swift/AutomaticHeightTagTableViewCell/ViewController.swift b/Swift/AutomaticHeightTagTableViewCell/ViewController.swift index bdb6a9c..84d459a 100644 --- a/Swift/AutomaticHeightTagTableViewCell/ViewController.swift +++ b/Swift/AutomaticHeightTagTableViewCell/ViewController.swift @@ -81,5 +81,17 @@ class ViewController: UITableViewController { let v = view as! UITableViewHeaderFooterView v.textLabel?.textColor = UIColor.darkGray } + + // MARK: - UIViewControllerTransitionCoordinator + + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransition(to: size, with: coordinator) + + coordinator.animate(alongsideTransition: { (context) in + self.tableView.reloadData() + }) { (context) in + + } + } } diff --git a/Swift/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.swift b/Swift/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.swift index 2231da3..3a2e393 100644 --- a/Swift/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.swift +++ b/Swift/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.swift @@ -30,20 +30,6 @@ struct AHTag { self.enabled = (dictionary["ENABLED"] as! Bool) } - func attributedTitle() -> NSAttributedString { - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.alignment = .center - paragraphStyle.firstLineHeadIndent = 10 - paragraphStyle.headIndent = 10 - paragraphStyle.tailIndent = 10 - - let attributes = [ - NSParagraphStyleAttributeName : paragraphStyle, - NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14) - ] - return NSAttributedString(string: self.title, attributes: attributes) - } - } class AHTagsLabel: UILabel { @@ -127,12 +113,19 @@ class AHTagsLabel: UILabel { let cell = UITableViewCell() for (_, tag) in tags.enumerated() { let view = AHTagView() - view.label.attributedText = tag.attributedTitle() + view.label.attributedText = AHTagsLabel.attributedTitle(string: tag.title) view.label.backgroundColor = tag.enabled ? tag.color : UIColor.lightGray let size = view.systemLayoutSizeFitting(view.frame.size, withHorizontalFittingPriority: UILayoutPriorityFittingSizeLevel, verticalFittingPriority: UILayoutPriorityFittingSizeLevel) - view.frame = CGRect(x: 0, y: 0, width: size.width + 20, height: size.height) + let width = min(size.width + 20, UIScreen.main.bounds.size.width - 15) + view.frame = CGRect(x: 0, y: 0, width: width, height: size.height) + if size.width + 20 > width { + let offset = CGFloat(tag.title.characters.count) * (UIScreen.main.bounds.size.width - 15)/(size.width + 20) - 2 + let to = tag.title.index(tag.title.startIndex, offsetBy: Int(offset)) + let string = String(format: "%@...", tag.title.substring(to: to)) + view.label.attributedText = AHTagsLabel.attributedTitle(string: string) + } cell.contentView.addSubview(view) let image = view.image() @@ -148,4 +141,17 @@ class AHTagsLabel: UILabel { self.attributedText = mutableString } + private class func attributedTitle(string: String) -> NSAttributedString { + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.alignment = .center + paragraphStyle.firstLineHeadIndent = 10 + paragraphStyle.headIndent = 10 + paragraphStyle.tailIndent = 10 + + let attributes = [ + NSParagraphStyleAttributeName : paragraphStyle, + NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14) + ] + return NSAttributedString(string: string, attributes: attributes) + } }