Skip to content

Commit

Permalink
Merge pull request #49 from redbooth/master
Browse files Browse the repository at this point in the history
Enable selection of which corners to round
  • Loading branch information
zekunyan committed Jun 15, 2018
2 parents f7f8a42 + e425cae commit 6de5dd1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Example/TTGTagCollectionView/TTGExample1ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (void)viewDidLoad {
config.tagShadowOpacity = 0.3f;
config.tagShadowRadius = 0.5f;

config.tagCornerRadius = 2;
config.tagCornerRadius = 7;

// Style2
config = _textTagCollectionView2.defaultConfig;
Expand All @@ -88,6 +88,10 @@ - (void)viewDidLoad {

config.tagCornerRadius = 8.0f;
config.tagSelectedCornerRadius = 4.0f;
config.roundBottomRight = true;
config.roundBottomLeft = true;
config.roundTopRight = false;
config.roundTopLeft = false;

config.tagBorderWidth = 0;

Expand Down
6 changes: 5 additions & 1 deletion TTGTagCollectionView/Classes/TTGTextTagCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/// TTGTextTagConfig

@interface TTGTextTagConfig : NSObject
@interface TTGTextTagConfig : NSObject;
// Text font
@property (strong, nonatomic) UIFont *tagTextFont;

Expand All @@ -32,6 +32,10 @@
// Corner radius
@property (assign, nonatomic) CGFloat tagCornerRadius;
@property (assign, nonatomic) CGFloat tagSelectedCornerRadius;
@property (assign, nonatomic) Boolean roundTopRight;
@property (assign, nonatomic) Boolean roundTopLeft;
@property (assign, nonatomic) Boolean roundBottomRight;
@property (assign, nonatomic) Boolean roundBottomLeft;

// Border
@property (assign, nonatomic) CGFloat tagBorderWidth;
Expand Down
74 changes: 66 additions & 8 deletions TTGTagCollectionView/Classes/TTGTextTagCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ - (instancetype)init {
_tagSelectedGradientBackgroundEndColor = [UIColor clearColor];
_tagGradientStartPoint = CGPointMake(0.5, 0.0);
_tagGradientEndPoint = CGPointMake(0.5, 1.0);

_tagCornerRadius = 4.0f;
_tagSelectedCornerRadius = 4.0f;

_roundTopLeft = true;
_roundTopRight = true;
_roundBottomLeft = true;
_roundBottomRight = true;

_tagBorderWidth = 1.0f;
_tagSelectedBorderWidth = 1.0f;

Expand Down Expand Up @@ -70,6 +74,10 @@ - (instancetype)copyWithZone:(NSZone *)zone {

newConfig.tagCornerRadius = _tagCornerRadius;
newConfig.tagSelectedCornerRadius = _tagSelectedCornerRadius;
newConfig.roundTopLeft = _roundTopLeft;
newConfig.roundTopRight = _roundTopRight;
newConfig.roundBottomLeft = _roundBottomLeft;
newConfig.roundBottomRight = _roundBottomRight;

newConfig.tagBorderWidth = _tagBorderWidth;
newConfig.tagSelectedBorderWidth = _tagSelectedBorderWidth;
Expand Down Expand Up @@ -613,8 +621,59 @@ - (void)updateAllLabelStyleAndFrame {
}

- (void)updateStyleAndFrameForLabel:(TTGTextTagLabel *)label {
// Update style
[super layoutSubviews];

TTGTextTagConfig *config = label.config;


UIRectCorner corners = -1;
if (config.roundTopLeft) {
if (corners == -1) {
corners = UIRectCornerTopLeft;
} else {
corners = corners | UIRectCornerTopLeft;
}
}

if (config.roundTopRight) {
if (corners == -1) {
corners = UIRectCornerTopRight;
} else {
corners = corners | UIRectCornerTopRight;
}
}

if (config.roundBottomLeft) {
if (corners == -1) {
corners = UIRectCornerBottomLeft;
} else {
corners = corners | UIRectCornerBottomLeft;
}
}

if (config.roundBottomRight) {
if (corners == -1) {
corners = UIRectCornerBottomRight;
} else {
corners = corners | UIRectCornerBottomRight;
}
}

CGFloat currentCornerRadius = label.selected ? config.tagSelectedCornerRadius : config.tagCornerRadius;

UIBezierPath *maskPath = [UIBezierPath
bezierPathWithRoundedRect:label.bounds
byRoundingCorners: corners
cornerRadii:CGSizeMake(currentCornerRadius, currentCornerRadius)
];

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = label.bounds;
maskLayer.path = maskPath.CGPath;

label.layer.mask = maskLayer;

label.label.font = config.tagTextFont;
label.label.textColor = label.selected ? config.tagSelectedTextColor : config.tagTextColor;
label.label.backgroundColor = label.selected ? config.tagSelectedBackgroundColor : config.tagBackgroundColor;
Expand All @@ -632,30 +691,29 @@ - (void)updateStyleAndFrameForLabel:(TTGTextTagLabel *)label {
((CAGradientLayer *)label.label.layer).endPoint = config.tagGradientEndPoint;
}

label.label.layer.cornerRadius = label.selected ? config.tagSelectedCornerRadius : config.tagCornerRadius;
label.label.layer.borderWidth = label.selected ? config.tagSelectedBorderWidth : config.tagBorderWidth;
label.label.layer.borderColor = (label.selected && config.tagSelectedBorderColor) ? config.tagSelectedBorderColor.CGColor : config.tagBorderColor.CGColor;
label.label.layer.masksToBounds = YES;

label.layer.shadowColor = (config.tagShadowColor ?: [UIColor clearColor]).CGColor;
label.layer.shadowOffset = config.tagShadowOffset;
label.layer.shadowRadius = config.tagShadowRadius;
label.layer.shadowOpacity = config.tagShadowOpacity;
label.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:label.bounds cornerRadius:label.label.layer.cornerRadius].CGPath;
label.layer.shouldRasterize = YES;
[label.layer setRasterizationScale:[[UIScreen mainScreen] scale]];

// Update frame
CGSize size = [label sizeThatFits:CGSizeZero];
size.width += config.tagExtraSpace.width;
size.height += config.tagExtraSpace.height;

// Width limit for vertical scroll direction
if (self.scrollDirection == TTGTagCollectionScrollDirectionVertical &&
size.width > (CGRectGetWidth(self.bounds) - self.contentInset.left - self.contentInset.right)) {
size.width = (CGRectGetWidth(self.bounds) - self.contentInset.left - self.contentInset.right);
}

label.frame = (CGRect){label.frame.origin, size};
}

Expand Down

0 comments on commit 6de5dd1

Please sign in to comment.