Skip to content
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
16 changes: 16 additions & 0 deletions MEBadgeFactoryType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// MEBadgeFactoryType.h
//
//
// Created by Victor Shamanov on 8/11/15.
//
//

#import <Foundation/Foundation.h>
#import "MEBadgeType.h"

@protocol MEBadgeFactoryType <NSObject>

- (id <MEBadgeType>)customBadgeWithString:(NSString *)string;

@end
11 changes: 11 additions & 0 deletions MESegmentedControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
5502CF77156B4CE200C4AC00 /* MEViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5502CF76156B4CE200C4AC00 /* MEViewController.m */; };
5502CF81156B4D0500C4AC00 /* MESegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 5502CF7E156B4D0500C4AC00 /* MESegmentedControl.m */; };
5502CF82156B4D0500C4AC00 /* CustomBadge.m in Sources */ = {isa = PBXBuildFile; fileRef = 5502CF80156B4D0500C4AC00 /* CustomBadge.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E6D653321B7A8D3B004A466B /* MEBadgeFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = E6D653311B7A8D3B004A466B /* MEBadgeFactory.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -37,6 +38,10 @@
5502CF7E156B4D0500C4AC00 /* MESegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MESegmentedControl.m; sourceTree = "<group>"; };
5502CF7F156B4D0500C4AC00 /* CustomBadge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomBadge.h; sourceTree = "<group>"; };
5502CF80156B4D0500C4AC00 /* CustomBadge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomBadge.m; sourceTree = "<group>"; };
E6D6532E1B7A8C9F004A466B /* MEBadgeType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MEBadgeType.h; sourceTree = "<group>"; };
E6D6532F1B7A8CC3004A466B /* MEBadgeFactoryType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = MEBadgeFactoryType.h; path = MESegmentedControl.xcodeproj/../MEBadgeFactoryType.h; sourceTree = SOURCE_ROOT; };
E6D653301B7A8D3B004A466B /* MEBadgeFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MEBadgeFactory.h; sourceTree = "<group>"; };
E6D653311B7A8D3B004A466B /* MEBadgeFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MEBadgeFactory.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -108,10 +113,14 @@
5502CF83156B4D2000C4AC00 /* MESegmentedControl Library */ = {
isa = PBXGroup;
children = (
E6D653301B7A8D3B004A466B /* MEBadgeFactory.h */,
E6D653311B7A8D3B004A466B /* MEBadgeFactory.m */,
5502CF7D156B4D0500C4AC00 /* MESegmentedControl.h */,
5502CF7E156B4D0500C4AC00 /* MESegmentedControl.m */,
5502CF7F156B4D0500C4AC00 /* CustomBadge.h */,
5502CF80156B4D0500C4AC00 /* CustomBadge.m */,
E6D6532F1B7A8CC3004A466B /* MEBadgeFactoryType.h */,
E6D6532E1B7A8C9F004A466B /* MEBadgeType.h */,
);
name = "MESegmentedControl Library";
sourceTree = "<group>";
Expand Down Expand Up @@ -179,6 +188,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E6D653321B7A8D3B004A466B /* MEBadgeFactory.m in Sources */,
5502CF6D156B4CE200C4AC00 /* main.m in Sources */,
5502CF71156B4CE200C4AC00 /* MEAppDelegate.m in Sources */,
5502CF77156B4CE200C4AC00 /* MEViewController.m in Sources */,
Expand Down Expand Up @@ -295,6 +305,7 @@
5502CF7C156B4CE200C4AC00 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
3 changes: 2 additions & 1 deletion MESegmentedControl/CustomBadge.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "MEBadgeType.h"

@interface CustomBadge : UIView {
@interface CustomBadge : UIView <MEBadgeType> {

NSString *badgeText;
UIColor *badgeTextColor;
Expand Down
5 changes: 5 additions & 0 deletions MESegmentedControl/CustomBadge.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ - (void) drawFrameWithContext:(CGContextRef)context withRect:(CGRect)rect;

@implementation CustomBadge

@dynamic frame;
@synthesize badgeText;
@synthesize badgeTextColor;
@synthesize badgeInsetColor;
Expand All @@ -39,6 +40,10 @@ @implementation CustomBadge
@synthesize badgeScaleFactor;
@synthesize badgeShining;

- (void)addToView:(UIView *)view {
[view addSubview:self];
}

// I recommend to use the allocator customBadgeWithString
- (id) initWithString:(NSString *)badgeString withScale:(CGFloat)scale withShining:(BOOL)shining
{
Expand Down
15 changes: 15 additions & 0 deletions MESegmentedControl/MEBadgeFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// MEBadgeFactory.h
//
//
// Created by Victor Shamanov on 8/11/15.
//
//

#import <Foundation/Foundation.h>

#import "MEBadgeFactoryType.h"

@interface MEBadgeFactory : NSObject <MEBadgeFactoryType>

@end
18 changes: 18 additions & 0 deletions MESegmentedControl/MEBadgeFactory.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// MEBadgeFactory.m
//
//
// Created by Victor Shamanov on 8/11/15.
//
//

#import "MEBadgeFactory.h"
#import "CustomBadge.h"

@implementation MEBadgeFactory

- (id<MEBadgeType>)customBadgeWithString:(NSString *)string {
return [CustomBadge customBadgeWithString:string];
}

@end
20 changes: 20 additions & 0 deletions MESegmentedControl/MEBadgeType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MEBadgeType.h
//
//
// Created by Victor Shamanov on 8/11/15.
//
//

#import <Foundation/Foundation.h>

@protocol MEBadgeType <NSObject>

@property (nonatomic, assign, readwrite) CGRect frame;

- (void)removeFromSuperview;
- (void)autoBadgeSizeWithString:(NSString *)string;

- (void)addToView:(UIView *)view;

@end
10 changes: 7 additions & 3 deletions MESegmentedControl/MESegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
// Copyright (c) 2012 David Thompson. All rights reserved.
//

#import "MEBadgeFactoryType.h"

@class CustomBadge;

@interface MESegmentedControl : UISegmentedControl
{
@interface MESegmentedControl : UISegmentedControl {
@private
NSMutableArray *_segmentBadgeNumbers;
NSMutableArray *_segmentBadges;
UIView *_badgeView;
}

// By default is MEBadgeFactory
@property (nonatomic, strong) id <MEBadgeFactoryType> badgeFactory;

// Set the badge number for a specific segment.
// Setting the badge number to 0 will clear the badge for that segment.
// Use the block to make adjustments to badge formatting, using the methods outlined in CustomBadge.h.
- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex usingBlock:(void(^)(CustomBadge *))configureBadge;
- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex usingBlock:(void(^)(id <MEBadgeType>))configureBadge;

// Convenience method for setting a badge number with default look and feel.
- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex;
Expand Down
26 changes: 18 additions & 8 deletions MESegmentedControl/MESegmentedControl.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#import "MESegmentedControl.h"
#import "CustomBadge.h"

//
// MESegmentedControl.m
//
// Created by David Thompson on 21/05/2012.
// Copyright (c) 2012 David Thompson. All rights reserved.
//

#import "MESegmentedControl.h"
#import "MEBadgeFactory.h"

@implementation MESegmentedControl

- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex usingBlock:(void(^)(CustomBadge *))configureBadge
@synthesize badgeFactory = _badgeFactory;

- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex usingBlock:(void(^)(id <MEBadgeType>))configureBadge
{
// If this is the first time a badge number has been set, then initialise the badges
if (_segmentBadgeNumbers.count == 0)
Expand Down Expand Up @@ -39,11 +41,11 @@ - (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)seg
if ((oldBadgeNumber == 0) && (badgeNumber > 0))
{
// Add a badge, positioned on the upper right side of the requested segment
// (Assumes that all segments are the same size - if segments are of different sizes, modify the below to use the widthForSegmentAtIndex method on UISegmentedControl)
CustomBadge *customBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:@"%d", badgeNumber]];
id <MEBadgeType> customBadge = [self.badgeFactory customBadgeWithString:[NSString stringWithFormat:@"%d", badgeNumber]];
[customBadge setFrame:CGRectMake(((self.frame.size.width/self.numberOfSegments) * (segmentIndex + 1))-customBadge.frame.size.width +5, -5, customBadge.frame.size.width, customBadge.frame.size.height)];

[_segmentBadges replaceObjectAtIndex:segmentIndex withObject:customBadge];
[_badgeView addSubview:customBadge];
[customBadge addToView:_badgeView];
}
else if ((oldBadgeNumber > 0) && (badgeNumber == 0))
{
Expand All @@ -66,7 +68,7 @@ - (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)seg

- (void)setBadgeNumber:(NSUInteger)badgeNumber forSegmentAtIndex:(NSUInteger)segmentIndex
{
[self setBadgeNumber:badgeNumber forSegmentAtIndex:segmentIndex usingBlock:^(CustomBadge *badge){}];
[self setBadgeNumber:badgeNumber forSegmentAtIndex:segmentIndex usingBlock:^(id <MEBadgeType> badge){}];
}

- (NSUInteger)getBadgeNumberForSegmentAtIndex:(NSUInteger)segmentIndex
Expand Down Expand Up @@ -107,4 +109,12 @@ -(void)dealloc
if (_badgeView) [_badgeView removeFromSuperview];
}

- (id <MEBadgeFactoryType>)badgeFactory {
if (_badgeFactory == nil) {
_badgeFactory = [[MEBadgeFactory alloc] init];
}

return _badgeFactory;
}

@end