Skip to content

Commit

Permalink
Title support for buttons #11
Browse files Browse the repository at this point in the history
  • Loading branch information
JaNd3r committed Sep 1, 2017
1 parent 9aaca6f commit 179cc1c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CKCircleMenuView/CKCircleMenuView.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern NSString* const CIRCLE_MENU_BACKGROUND_BLUR;
extern NSString* const CIRCLE_MENU_BUTTON_TINT;
extern NSString* const CIRCLE_MENU_ALLOW_ANIMATION_INTERACTION;
extern NSString* const CIRCLE_MENU_STARTING_ANGLE;
extern NSString* const CIRCLE_MENU_BUTTON_TITLE_VISIBLE;
extern NSString* const CIRCLE_MENU_BUTTON_TITLE_FONT_SIZE;

typedef enum {
CircleMenuDirectionUp = 1,
Expand Down Expand Up @@ -77,6 +79,18 @@ typedef enum {
*/
- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary*)anOptionsDictionary withImageArray:(NSArray*)anImageArray;

/**
* Initializes the CKCircleMenuView.
* @param aPoint the center of the menu's circle
* @param anOptionsDictionary optional configuration, may be nil
* @param anImageArray array of images to be used for the buttons,
* currently icon images should be 32x32 points
* (64x64 px for retina)
* @param aTitleArray array of strings with titles that are displayed
* below the button icons
*/
- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary*)anOptionsDictionary withImageArray:(NSArray*)anImageArray andTitles:(NSArray*)aTitleArray;

/**
* Opens the menu with the buttons and settings specified in the
* initializer.
Expand Down
44 changes: 40 additions & 4 deletions CKCircleMenuView/CKCircleMenuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ @interface CKCircleMenuView()
@property (nonatomic) BOOL visualFxMode;
@property (nonatomic) BOOL buttonTintMode;
@property (nonatomic) BOOL allowAnimationInteraction;
@property (nonatomic) BOOL buttonTitleVisible;
@property (nonatomic) CGFloat buttonTitleFontSize;

@property (nonatomic, weak) UIView* clippingView;

Expand Down Expand Up @@ -56,6 +58,8 @@ @interface CKCircleMenuView()
NSString* const CIRCLE_MENU_BUTTON_TINT = @"kCircleMenuButtonTint";
NSString* const CIRCLE_MENU_ALLOW_ANIMATION_INTERACTION = @"kCircleMenuAllowAnimationInteraction";
NSString* const CIRCLE_MENU_STARTING_ANGLE = @"kCircleMenuStartingAngle";
NSString* const CIRCLE_MENU_BUTTON_TITLE_VISIBLE = @"kCircleMenuButtonTitleVisible";
NSString* const CIRCLE_MENU_BUTTON_TITLE_FONT_SIZE = @"kCircleMenuButtonTitleFontSize";

@implementation CKCircleMenuView

Expand Down Expand Up @@ -100,6 +104,11 @@ - (id)initWithOptions:(NSDictionary*)anOptionsDictionary
self.visualFxMode = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BACKGROUND_BLUR] boolValue];
self.buttonTintMode = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_TINT] boolValue];
self.allowAnimationInteraction = [[anOptionsDictionary valueForKey:CIRCLE_MENU_ALLOW_ANIMATION_INTERACTION] boolValue];
self.buttonTitleVisible = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_TITLE_VISIBLE] boolValue];
self.buttonTitleFontSize = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_TITLE_FONT_SIZE] doubleValue];
if (!self.buttonTitleFontSize) {
self.buttonTitleFontSize = 11.0;
}
} else {
// using some default settings
self.innerViewColor = [UIColor colorWithRed:0.0 green:0.25 blue:0.5 alpha:1.0];
Expand All @@ -117,22 +126,35 @@ - (id)initWithOptions:(NSDictionary*)anOptionsDictionary
self.visualFxMode = NO;
self.buttonTintMode = NO;
self.allowAnimationInteraction = NO;
self.buttonTitleVisible = NO;
self.buttonTitleFontSize = 11.0;
}
}
return self;
}

- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDictionary withImageArray:(NSArray *)anImageArray
{
self = [self initAtOrigin:aPoint usingOptions:anOptionsDictionary withImageArray:anImageArray andTitles:@[]];
return self;
}

- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary*)anOptionsDictionary withImageArray:(NSArray*)anImageArray andTitles:(NSArray*)aTitleArray;
{
self = [self initWithOptions:anOptionsDictionary];
if (self) {
int tTag = 1;
for (UIImage* img in anImageArray) {
UIView* tView = [self createButtonViewWithImage:img andTag:tTag];
UIView* tView;
if ([aTitleArray count] >= tTag) {
tView = [self createButtonViewWithImage:img andTag:tTag andTitle:aTitleArray[tTag-1]];
} else {
tView = [self createButtonViewWithImage:img andTag:tTag andTitle:nil];
}
[self.buttons addObject:tView];
tTag += 1;
}

self.frame = [self calculateFrameWithOrigin:aPoint];
}
return self;
Expand All @@ -146,7 +168,7 @@ - (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDiction
va_list args;
va_start(args, anImage);
for (UIImage* img = anImage; img != nil; img = va_arg(args, UIImage*)) {
UIView* tView = [self createButtonViewWithImage:img andTag:tTag];
UIView* tView = [self createButtonViewWithImage:img andTag:tTag andTitle:nil];
[self.buttons addObject:tView];
tTag += 1;
}
Expand Down Expand Up @@ -188,7 +210,7 @@ - (CGRect)calculateFrameWithOrigin:(CGPoint)aPoint {
* @param aTag unique identifier (should be index + 1)
* @return UIView to be used as button
*/
- (UIView*)createButtonViewWithImage:(UIImage*)anImage andTag:(int)aTag
- (UIView*)createButtonViewWithImage:(UIImage*)anImage andTag:(int)aTag andTitle:(NSString*)aTitle
{
UIButton* tButton;
if (self.buttonTintMode) {
Expand Down Expand Up @@ -220,6 +242,20 @@ - (UIView*)createButtonViewWithImage:(UIImage*)anImage andTag:(int)aTag
[self applyInactiveDepthToButtonView:tInnerView];
}

// Support for displaying button titles
if (self.buttonTitleVisible && aTitle) {
UILabel* tLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, self.buttonRadius * 2 - self.buttonTitleFontSize * 2.15, self.buttonRadius * 2, self.buttonTitleFontSize + 2.0)];
tLabel.textAlignment = NSTextAlignmentCenter;
tLabel.font = [UIFont systemFontOfSize:self.buttonTitleFontSize];
tLabel.textColor = self.borderViewColor;
tLabel.text = aTitle;
tLabel.userInteractionEnabled = NO;
[tInnerView addSubview:tLabel];

// Make some space for label below button
tButton.layer.affineTransform = CGAffineTransformMakeTranslation(0.0, -self.buttonTitleFontSize / 1.9);
}

tInnerView.tag = aTag + TAG_INNER_VIEW_OFFSET;
[tInnerView addSubview:tButton];

Expand Down
3 changes: 2 additions & 1 deletion CircleMenuDemo/CircleMenuDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ - (IBAction)longPressGestureRecognized:(UILongPressGestureRecognizer *)sender
[tOptions setValue:[NSNumber numberWithBool:NO] forKey:CIRCLE_MENU_LINE_MODE];
[tOptions setValue:[NSNumber numberWithBool:NO] forKey:CIRCLE_MENU_BACKGROUND_BLUR];
[tOptions setValue:[NSNumber numberWithBool:NO] forKey:CIRCLE_MENU_BUTTON_TINT];
[tOptions setValue:[NSNumber numberWithBool:YES] forKey:CIRCLE_MENU_BUTTON_TITLE_VISIBLE];

CKCircleMenuView* tMenu = [[CKCircleMenuView alloc] initAtOrigin:tPoint usingOptions:tOptions withImageArray:self.imageArray];
CKCircleMenuView* tMenu = [[CKCircleMenuView alloc] initAtOrigin:tPoint usingOptions:tOptions withImageArray:self.imageArray andTitles:@[@"Trash"]];
tMenu.delegate = self;
[self.demoAreaLabel addSubview:tMenu];
[tMenu openMenuWithRecognizer:sender];
Expand Down

0 comments on commit 179cc1c

Please sign in to comment.