Skip to content

Commit

Permalink
#170 Add UIAppearance to support selectionIndexLabel round style
Browse files Browse the repository at this point in the history
  • Loading branch information
1and2papa committed Nov 23, 2015
1 parent eea4e82 commit 78eead8
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 55 deletions.
38 changes: 38 additions & 0 deletions CTAssetsPickerController/CTAssetSelectionLabel.h
@@ -0,0 +1,38 @@
/*
MIT License (MIT)
Copyright (c) 2015 Clement CN Tsang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#import <UIKit/UIKit.h>

@interface CTAssetSelectionLabel : UILabel

@property (nonatomic, assign, getter=isCircular) BOOL circular UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) CGFloat borderWidth UI_APPEARANCE_SELECTOR;
@property (nonatomic, weak) UIColor *borderColor UI_APPEARANCE_SELECTOR;

- (void)setMargin:(CGFloat)margin UI_APPEARANCE_SELECTOR;
- (void)setTextAttributes:(NSDictionary *)textAttributes UI_APPEARANCE_SELECTOR;

@end
144 changes: 144 additions & 0 deletions CTAssetsPickerController/CTAssetSelectionLabel.m
@@ -0,0 +1,144 @@
/*
MIT License (MIT)
Copyright (c) 2015 Clement CN Tsang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#import <PureLayout/PureLayout.h>
#import "CTAssetSelectionLabel.h"
#import "CTAssetsPickerDefines.h"



@interface CTAssetSelectionLabel ()

@property (nonatomic, strong) NSLayoutConstraint *constraint1;
@property (nonatomic, strong) NSLayoutConstraint *constraint2;

@property (nonatomic, assign) BOOL didSetupConstraints;

@end



@implementation CTAssetSelectionLabel

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

if (self)
{
self.textAlignment = NSTextAlignmentCenter;
self.font = CTAssetLabelFont;
self.textColor = CTAssetLabelTextColor;
self.backgroundColor = CTAssetLabelBackgroundColor;
self.layer.borderColor = CTAssetLabelBorderColor.CGColor;
self.layer.masksToBounds = YES;
self.isAccessibilityElement = NO;
}

return self;
}

#pragma mark - Apperance

- (BOOL)isCircular
{
return (self.layer.cornerRadius > 0);
}

- (void)setCircular:(BOOL)circular
{
if (circular)
{
self.layer.cornerRadius = CTAssetLabelSize.height / 2.0;
}
else
{
self.layer.cornerRadius = 0.0;
}
}

- (CGFloat)borderWidth
{
return self.layer.borderWidth;
}

- (void)setBorderWidth:(CGFloat)borderWidth
{
self.layer.borderWidth = borderWidth;
}

- (UIColor *)borderColor
{
return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setBorderColor:(UIColor *)borderColor
{
UIColor *color = (borderColor) ? borderColor : CTAssetLabelBorderColor;
self.layer.borderColor = color.CGColor;
}

- (void)setMargin:(CGFloat)margin
{
[self.superview removeConstraints:@[self.constraint1, self.constraint2]];

self.constraint1 = [self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:margin];
self.constraint2 = [self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:margin];
}

- (void)setTextAttributes:(NSDictionary *)textAttributes
{
UIFont *font = (textAttributes) ? [textAttributes objectForKey:NSFontAttributeName] : CTAssetLabelFont;
self.font = font;

UIColor *textColor = (textAttributes) ? [textAttributes objectForKey:NSForegroundColorAttributeName] : CTAssetLabelTextColor;
self.textColor = textColor;

UIColor *backgroundColor = (textAttributes) ? [textAttributes objectForKey:NSBackgroundColorAttributeName] : CTAssetLabelBackgroundColor;
self.backgroundColor = backgroundColor;
}


#pragma mark - Update auto layout constraints

- (void)updateConstraints
{
if (!self.didSetupConstraints)
{
[NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
[self autoSetDimensionsToSize:CTAssetLabelSize];
}];

self.constraint1 = [self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
self.constraint2 = [self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];

self.didSetupConstraints = YES;
}

[super updateConstraints];
}

@end
4 changes: 2 additions & 2 deletions CTAssetsPickerController/CTAssetsGridSelectedView.h
Expand Up @@ -32,8 +32,8 @@
@property (nonatomic, assign) NSUInteger selectionIndex;

@property (nonatomic, weak) UIColor *selectedBackgroundColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, weak) UIFont *font UI_APPEARANCE_SELECTOR;
@property (nonatomic, weak) UIColor *textColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, weak) UIFont *font UI_APPEARANCE_SELECTOR DEPRECATED_MSG_ATTRIBUTE("Set text attributes to CTAssetSelectionLabel instead.");
@property (nonatomic, weak) UIColor *textColor UI_APPEARANCE_SELECTOR DEPRECATED_MSG_ATTRIBUTE("Set text attributes to CTAssetSelectionLabel instead.");
@property (nonatomic, assign) CGFloat borderWidth UI_APPEARANCE_SELECTOR;

@end
42 changes: 3 additions & 39 deletions CTAssetsPickerController/CTAssetsGridSelectedView.m
Expand Up @@ -28,14 +28,15 @@ of this software and associated documentation files (the "Software"), to deal
#import "CTAssetsPickerDefines.h"
#import "CTAssetsGridSelectedView.h"
#import "CTAssetCheckmark.h"
#import "CTAssetSelectionLabel.h"




@interface CTAssetsGridSelectedView ()

@property (nonatomic, strong) CTAssetCheckmark *checkmark;
@property (nonatomic, strong) UILabel *selectionIndexLabel;
@property (nonatomic, strong) CTAssetSelectionLabel *selectionIndexLabel;

@property (nonatomic, assign) BOOL didSetupConstraints;

Expand Down Expand Up @@ -70,11 +71,7 @@ - (void)setupViews
self.checkmark = checkmark;
[self addSubview:checkmark];

UILabel *selectionIndexLabel = [UILabel newAutoLayoutView];
selectionIndexLabel.textAlignment = NSTextAlignmentCenter;
selectionIndexLabel.backgroundColor = self.tintColor;
selectionIndexLabel.font = CTAssetsGridSelectedViewFont;
selectionIndexLabel.textColor = CTAssetsGridSelectedViewTextColor;
CTAssetSelectionLabel *selectionIndexLabel = [CTAssetSelectionLabel newAutoLayoutView];
self.selectionIndexLabel = selectionIndexLabel;

[self addSubview:self.selectionIndexLabel];
Expand All @@ -94,28 +91,6 @@ - (void)setSelectedBackgroundColor:(UIColor *)backgroundColor
self.backgroundColor = color;
}

- (UIFont *)font
{
return self.selectionIndexLabel.font;
}

- (void)setFont:(UIFont *)font
{
UIFont *labelFont = (font) ? font : CTAssetsGridSelectedViewFont;
self.selectionIndexLabel.font = labelFont;
}

- (UIColor *)textColor
{
return self.selectionIndexLabel.textColor;
}

- (void)setTextColor:(UIColor *)textColor
{
UIColor *color = (textColor) ? textColor : CTAssetsGridSelectedViewTextColor;
self.selectionIndexLabel.textColor = color;
}

- (CGFloat)borderWidth
{
return self.layer.borderWidth;
Expand All @@ -129,11 +104,9 @@ - (void)setBorderWidth:(CGFloat)borderWidth
- (void)setTintColor:(UIColor *)tintColor
{
UIColor *color = (tintColor) ? tintColor : CTAssetsGridSelectedViewTintColor;
self.selectionIndexLabel.backgroundColor = color;
self.layer.borderColor = color.CGColor;
}


#pragma mark - Accessors

- (void)setShowsSelectionIndex:(BOOL)showsSelectionIndex
Expand Down Expand Up @@ -165,15 +138,6 @@ - (void)updateConstraints
{
if (!self.didSetupConstraints)
{
[NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
CGFloat height = self.selectionIndexLabel.font.pointSize + self.layoutMargins.top;
[self.selectionIndexLabel autoSetDimension:ALDimensionHeight toSize:height];
[self.selectionIndexLabel autoConstrainAttribute:ALAttributeWidth toAttribute:ALAttributeHeight ofView:self.selectionIndexLabel];
}];

[self.selectionIndexLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
[self.selectionIndexLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];

[self.checkmark autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
[self.checkmark autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];

Expand Down
2 changes: 0 additions & 2 deletions CTAssetsPickerController/CTAssetsPickerController.h
Expand Up @@ -124,8 +124,6 @@
*
* Only a checkmark is shown on selected assets by default. To shows the order of selection,
* set this property’s value to `YES`.
*
* It is also suggested to add border width of the selected grids. Please refer to the demo app on adding border.
*/
@property (nonatomic, assign) BOOL showsSelectionIndex;

Expand Down
8 changes: 6 additions & 2 deletions CTAssetsPickerController/CTAssetsPickerDefines.h
Expand Up @@ -54,8 +54,12 @@

#define CTAssetsGridSelectedViewBackgroundColor [UIColor colorWithWhite:1 alpha:0.3]
#define CTAssetsGridSelectedViewTintColor [UIView new].tintColor
#define CTAssetsGridSelectedViewFont [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
#define CTAssetsGridSelectedViewTextColor [UIColor whiteColor]

#define CTAssetLabelSize CGSizeMake(25.0f, 25.0f)
#define CTAssetLabelFont [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
#define CTAssetLabelTextColor [UIColor whiteColor]
#define CTAssetLabelBackgroundColor [UIView new].tintColor
#define CTAssetLabelBorderColor [UIColor whiteColor]

#define CTAssetsGridViewFooterFont [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
#define CTAssetsGridViewFooterTextColor [UIColor darkTextColor]
Expand Down
8 changes: 4 additions & 4 deletions CTAssetsPickerDemo.xcodeproj/project.pbxproj
Expand Up @@ -84,8 +84,8 @@
AD4B06CA1B44CD5A00D99C5A /* CTSortedAssetsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CTSortedAssetsViewController.m; sourceTree = "<group>"; };
AD4CC0CB1AD61E0D009C3F45 /* CTAssetsGridViewLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTAssetsGridViewLayout.h; sourceTree = "<group>"; };
AD4CC0CC1AD61E0D009C3F45 /* CTAssetsGridViewLayout.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTAssetsGridViewLayout.m; sourceTree = "<group>"; };
AD55619C1C02BFA300EA2E31 /* CTAssetsGridLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTAssetsGridLabel.h; sourceTree = "<group>"; };
AD55619D1C02BFA300EA2E31 /* CTAssetsGridLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTAssetsGridLabel.m; sourceTree = "<group>"; };
AD55619C1C02BFA300EA2E31 /* CTAssetSelectionLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTAssetSelectionLabel.h; sourceTree = "<group>"; };
AD55619D1C02BFA300EA2E31 /* CTAssetSelectionLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTAssetSelectionLabel.m; sourceTree = "<group>"; };
AD60C0FD1AB67D3A00F3C675 /* CTAssetThumbnailOverlay.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTAssetThumbnailOverlay.h; sourceTree = "<group>"; };
AD60C0FE1AB67D3A00F3C675 /* CTAssetThumbnailOverlay.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTAssetThumbnailOverlay.m; sourceTree = "<group>"; };
AD6CDA481AC55BEE008C6AC2 /* UICollectionView+CTAssetsPickerController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UICollectionView+CTAssetsPickerController.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,12 +308,12 @@
ADD966121AAD5780002A26A2 /* CTAssetsGridViewCell.m */,
AD18AFD21BCBB856008B507D /* CTAssetsGridSelectedView.h */,
AD18AFD31BCBB856008B507D /* CTAssetsGridSelectedView.m */,
AD55619C1C02BFA300EA2E31 /* CTAssetsGridLabel.h */,
AD55619D1C02BFA300EA2E31 /* CTAssetsGridLabel.m */,
ADD9660F1AAD5780002A26A2 /* CTAssetsGridViewFooter.h */,
ADD966101AAD5780002A26A2 /* CTAssetsGridViewFooter.m */,
AD9BB2481B295EE2001F9809 /* CTAssetCheckmark.h */,
AD9BB2491B295EE2001F9809 /* CTAssetCheckmark.m */,
AD55619C1C02BFA300EA2E31 /* CTAssetSelectionLabel.h */,
AD55619D1C02BFA300EA2E31 /* CTAssetSelectionLabel.m */,
ADD9660A1AAD5780002A26A2 /* CTAssetsPageViewController.h */,
ADD9660B1AAD5780002A26A2 /* CTAssetsPageViewController.m */,
AD7B28F51BCE2C88001EC414 /* CTAssetsPageView.h */,
Expand Down
25 changes: 19 additions & 6 deletions CTAssetsPickerDemo/Examples/CTApperanceViewController.m
Expand Up @@ -33,6 +33,7 @@ of this software and associated documentation files (the "Software"), to deal
#import <CTAssetsPickerController/CTAssetsGridViewCell.h>
#import <CTAssetsPickerController/CTAssetsGridSelectedView.h>
#import <CTAssetsPickerController/CTAssetCheckmark.h>
#import <CTAssetsPickerController/CTAssetSelectionLabel.h>
#import <CTAssetsPickerController/CTAssetsPageView.h>


Expand Down Expand Up @@ -118,14 +119,21 @@ - (void)viewDidLoad
CTAssetsGridSelectedView *assetsGridSelectedView = [CTAssetsGridSelectedView appearance];
assetsGridSelectedView.selectedBackgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
assetsGridSelectedView.tintColor = self.color1;
assetsGridSelectedView.font = [self.font fontWithSize:18.0];
assetsGridSelectedView.textColor = self.color2;
assetsGridSelectedView.borderWidth = 1.0;
assetsGridSelectedView.isLabelCircular = YES;


// check mark
[CTAssetCheckmark appearance].tintColor = self.color1;

// selection label
CTAssetSelectionLabel *assetSelectionLabel = [CTAssetSelectionLabel appearance];
assetSelectionLabel.circular = YES;
assetSelectionLabel.borderWidth = 1.0;
assetSelectionLabel.borderColor = self.color2;
[assetSelectionLabel setMargin:2.0];
[assetSelectionLabel setTextAttributes:@{NSFontAttributeName : [self.font fontWithSize:12.0],
NSForegroundColorAttributeName : self.color2,
NSBackgroundColorAttributeName : self.color1}];

// page view (preview)
CTAssetsPageView *assetsPageView = [CTAssetsPageView appearance];
assetsPageView.pageBackgroundColor = self.color3;
Expand Down Expand Up @@ -182,12 +190,17 @@ - (void)dealloc
CTAssetsGridSelectedView *assetsGridSelectedView = [CTAssetsGridSelectedView appearance];
assetsGridSelectedView.selectedBackgroundColor = nil;
assetsGridSelectedView.tintColor = nil;
assetsGridSelectedView.font = nil;
assetsGridSelectedView.textColor = nil;
assetsGridSelectedView.borderWidth = 0.0;

[CTAssetCheckmark appearance].tintColor = nil;

CTAssetSelectionLabel *assetSelectionLabel = [CTAssetSelectionLabel appearance];
assetSelectionLabel.circular = NO;
assetSelectionLabel.borderWidth = 0.0;
assetSelectionLabel.borderColor = nil;
[assetSelectionLabel setMargin:0.0];
[assetSelectionLabel setTextAttributes:nil];

CTAssetsPageView *assetsPageView = [CTAssetsPageView appearance];
assetsPageView.pageBackgroundColor = nil;
assetsPageView.fullscreenBackgroundColor = nil;
Expand Down

0 comments on commit 78eead8

Please sign in to comment.