Skip to content

Commit

Permalink
The tag is chopped if it's wider than the width of the display #9
Browse files Browse the repository at this point in the history
An experimenting approach
  • Loading branch information
weijentu committed May 3, 2017
1 parent b2afdf3 commit 811c04f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
8 changes: 7 additions & 1 deletion ObjC/AutomaticHeightTagTableViewCell/Models/TagGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions ObjC/AutomaticHeightTagTableViewCell/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView reloadData];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {}];
}

@end
12 changes: 10 additions & 2 deletions ObjC/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -92,11 +94,16 @@ - (void)setTags:(NSArray<AHTag *> *)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;
Expand All @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion Swift/AutomaticHeightTagTableViewCell/Models/TagGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions Swift/AutomaticHeightTagTableViewCell/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}
}

38 changes: 22 additions & 16 deletions Swift/AutomaticHeightTagTableViewCell/Views/AHTagsLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}

0 comments on commit 811c04f

Please sign in to comment.