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 alignment style for tab bar item title #53

Open
wants to merge 1 commit 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
10 changes: 10 additions & 0 deletions RDVTabBarController/RDVTabBarItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, RDVTabBarItemTitleVericalAlignment) {
RDVTabBarItemTitleVericalAlignmentIconBottom,
RDVTabBarItemTitleVericalAlignmentViewBottom
};

@interface RDVTabBarItem : UIControl

/**
Expand Down Expand Up @@ -57,6 +62,11 @@
*/
@property (copy) NSDictionary *selectedTitleAttributes;

/*
* The alignment for title label
*/
@property (assign) RDVTabBarItemTitleVericalAlignment titleAlignment;

#pragma mark - Image configuration

/**
Expand Down
22 changes: 20 additions & 2 deletions RDVTabBarController/RDVTabBarItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ - (void)commonInitialization {

_title = @"";
_titlePositionAdjustment = UIOffsetZero;
_titleAlignment = RDVTabBarItemTitleVericalAlignmentIconBottom;

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
_unselectedTitleAttributes = @{
Expand Down Expand Up @@ -96,7 +97,8 @@ - (void)drawRect:(CGRect)rect {
UIImage *backgroundImage = nil;
UIImage *image = nil;
CGFloat imageStartingY = 0.0f;

CGFloat titleStartingY = 0.0f;

if ([self isSelected]) {
image = [self selectedImage];
backgroundImage = [self selectedBackgroundImage];
Expand Down Expand Up @@ -143,9 +145,25 @@ - (void)drawRect:(CGRect)rect {

CGContextSetFillColorWithColor(context, [titleAttributes[NSForegroundColorAttributeName] CGColor]);

switch (self.titleAlignment) {
case RDVTabBarItemTitleVericalAlignmentIconBottom: {
titleStartingY = imageStartingY + imageSize.height + _titlePositionAdjustment.vertical;
break;
}
case RDVTabBarItemTitleVericalAlignmentViewBottom: {
static const CGFloat titleBottomOffset = 5.;
titleStartingY = frameSize.height - titleSize.height - titleBottomOffset;
break;
}
default: {
titleStartingY = imageStartingY + imageSize.height + _titlePositionAdjustment.vertical;
break;
}
}

[_title drawInRect:CGRectMake(roundf(frameSize.width / 2 - titleSize.width / 2) +
_titlePositionAdjustment.horizontal,
imageStartingY + imageSize.height + _titlePositionAdjustment.vertical,
titleStartingY,
titleSize.width, titleSize.height)
withAttributes:titleAttributes];
} else {
Expand Down