Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added labels below the buttons. #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ The border width for item views. Default 2.
@property (nonatomic, weak) id <RNFrostedSidebarDelegate> delegate;
```

The label font and color. Default to 14pt white system font. The keys are ```RNFrostedLabelFont``` and ```RNFrostedLabelColor```.
```objc
- (void)setLabelOptions:(NSDictionary*)options;
```


An optional delegate to respond to selection of item views. Optional delegate methods, provided by [George Villasboas](https://github.com/ghvillasboas), include:

```objc
Expand Down
6 changes: 6 additions & 0 deletions RNFrostedSidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

#import <UIKit/UIKit.h>

FOUNDATION_EXPORT NSString *const RNFrostedLabelFont;
FOUNDATION_EXPORT NSString *const RNFrostedLabelColor;

@class RNFrostedSidebar;

@protocol RNFrostedSidebarDelegate <NSObject>
Expand Down Expand Up @@ -66,6 +69,7 @@
// An optional delegate to respond to interaction events
@property (nonatomic, weak) id <RNFrostedSidebarDelegate> delegate;

- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices borderColors:(NSArray *)colors labelStrings:(NSArray*)labels;
- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices borderColors:(NSArray *)colors;
- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices;
- (instancetype)initWithImages:(NSArray *)images;
Expand All @@ -77,4 +81,6 @@
- (void)dismiss;
- (void)dismissAnimated:(BOOL)animated;

- (void)setLabelOptions:(NSDictionary*)options;

@end
45 changes: 42 additions & 3 deletions RNFrostedSidebar.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#import "RNFrostedSidebar.h"
#import <QuartzCore/QuartzCore.h>

NSString *const RNFrostedLabelFont = @"RNFrostedLabelFont";
NSString *const RNFrostedLabelColor = @"RNFrostedLabelColor";

#pragma mark - Categories

@implementation UIView (rn_Screenshot)
Expand Down Expand Up @@ -241,6 +244,7 @@ @interface RNFrostedSidebar ()
@property (nonatomic, strong) UIImageView *blurView;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic, strong) NSArray *images;
@property (nonatomic, strong) NSMutableArray *labels;
@property (nonatomic, strong) NSArray *borderColors;
@property (nonatomic, strong) NSMutableArray *itemViews;
@property (nonatomic, strong) NSMutableIndexSet *selectedIndices;
Expand All @@ -255,7 +259,8 @@ + (instancetype)visibleSidebar {
return rn_frostedMenu;
}

- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices borderColors:(NSArray *)colors {
- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices borderColors:(NSArray *)colors labelStrings:(NSArray*)labels
{
if (self = [super init]) {
_isSingleSelect = NO;
_contentView = [[UIScrollView alloc] init];
Expand All @@ -271,12 +276,17 @@ - (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)s
_itemSize = CGSizeMake(_width/2, _width/2);
_itemViews = [NSMutableArray array];
_tintColor = [UIColor colorWithWhite:0.2 alpha:0.73];
_labels = [@[] mutableCopy];
_borderWidth = 2;
_itemBackgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.25];

if (colors) {
NSAssert([colors count] == [images count], @"Border color count must match images count. If you want a blank border, use [UIColor clearColor].");
}

if (labels) {
NSAssert([labels count] == [images count], @"Label count must match images count. If you don't want a labeled button, use @\"\"");
}

_selectedIndices = [selectedIndices mutableCopy] ?: [NSMutableIndexSet indexSet];
_borderColors = colors;
Expand All @@ -287,9 +297,21 @@ - (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)s
view.itemIndex = idx;
view.clipsToBounds = YES;
view.imageView.image = image;

[_contentView addSubview:view];

[_itemViews addObject:view];

if (labels) {
UILabel* label = [[UILabel alloc] init];
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:14];
label.text = labels[idx];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
[_labels addObject:label];
[_contentView addSubview:label];
}

if (_borderColors && _selectedIndices && [_selectedIndices containsIndex:idx]) {
UIColor *color = _borderColors[idx];
Expand All @@ -303,6 +325,10 @@ - (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)s
return self;
}

- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices borderColors:(NSArray *)colors {
return [self initWithImages:images selectedIndices:selectedIndices borderColors:colors labelStrings:nil];
}

- (instancetype)initWithImages:(NSArray *)images selectedIndices:(NSIndexSet *)selectedIndices {
return [self initWithImages:images selectedIndices:selectedIndices borderColors:nil];
}
Expand All @@ -316,6 +342,14 @@ - (instancetype)init {
return nil;
}

- (void)setLabelOptions:(NSDictionary*)options
{
[self.labels enumerateObjectsUsingBlock:^(UILabel* label, NSUInteger idx, BOOL *stop) {
[label setFont:options[RNFrostedLabelFont]];
[label setTextColor:options[RNFrostedLabelColor]];
}];
}

- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor clearColor];
Expand Down Expand Up @@ -615,7 +649,12 @@ - (void)layoutItems {
view.frame = frame;
view.layer.cornerRadius = frame.size.width/2.f;
}];


[self.labels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
CGRect frame = CGRectMake(0, topPadding*idx + self.itemSize.height*(idx+1) + topPadding, self.width, 24);
label.frame = frame;
}];

NSInteger items = [self.itemViews count];
self.contentView.contentSize = CGSizeMake(0, items * (self.itemSize.height + leftPadding) + leftPadding);
}
Expand Down
24 changes: 22 additions & 2 deletions example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,29 @@ - (IBAction)onBurger:(id)sender {
[UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
[UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
];

RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:self.optionIndices borderColors:colors];

NSArray* labels = @[@"gear",
@"globe",
@"profile",
@"star",
@"gear",
@"globe",
@"profile",
@"star",
@"gear",
@"globe",
@"profile",
@"star"
];
RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:self.optionIndices borderColors:colors labelStrings:labels];
// RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:self.optionIndices borderColors:colors];
// RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images];

[callout setLabelOptions:@{
RNFrostedLabelFont: [UIFont systemFontOfSize:14],
RNFrostedLabelColor: [UIColor whiteColor]
}];

callout.delegate = self;
// callout.showFromRight = YES;
[callout show];
Expand Down