//
// CustomSegmentedControl.m
//
// Created by Yongho Ji on 10. 9. 15..
//
#import "CustomSegmentedControl.h"
@implementation CustomSegmentedControl
//UISegment계열 폰트의 색과 크기를 조절시켜준다. 재귀적으로 찾아가는 것을 눈여겨 보자.
- (void)_changeUISegmentFont:(UIView*) aView
fontSize:(int)fontsize
textColor:(UIColor*)textcolor
{
NSString *typeName = NSStringFromClass([aView class]);
if([typeName compare:@"UISegmentLabel" options:NSLiteralSearch] == NSOrderedSame) {
UILabel *label = (UILabel*)aView;
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
[label setFont:font]; //글자크기 지정
[label setTextColor:textcolor]; //글자색 지정
//글자크기에 따라 위치/크기 보정
CGSize size = [label.text sizeWithFont:font forWidth:320 lineBreakMode:UILineBreakModeClip];
[label setFrame:CGRectMake(0, 0, size.width, size.height)];
[label setCenter:CGPointMake(label.superview.frame.size.width/2, label.superview.frame.size.height/2)];
}
NSArray *subs = [aView subviews];
NSEnumerator* iter = [subs objectEnumerator];
UIView *subView;
while (subView = [iter nextObject]) {
[self _changeUISegmentFont:subView fontSize:fontsize textColor:textcolor];
}
}
//색이 바뀔때마다 Segment의 배경색과 폰트색을 바꿔준다.
-(void)_setToggleHiliteColors {
//NSLog(@"%d",self.selectedSegmentIndex);
int index = self.selectedSegmentIndex;
int numSegments = [self.subviews count];
id subview;
//리셋 및 선택 처리
// 깜박임이 존재하는 것은 UISegmentedControl 내부적으로 폰트 및 색을 그렸다가
// 여기서 또 강제로 다시한번 지정하기 때문에 그렇다.
// 어찌할 방법을 찾지는 못했지만 그럭저럭 쓸만함
for (int i=0; i<numSegments; i++) {
subview = [self viewWithTag:i];
if (i==index) { //선택
[subview setTintColor:nil];
[subview setTintColor:onColor];
[self _changeUISegmentFont:subview fontSize:fontSize textColor:onTextColor];
} else { //리셋
[subview setTintColor:nil];
[subview setTintColor:offColor];
[self _changeUISegmentFont:subview fontSize:fontSize textColor:offTextColor];
}
}
}
//초기화 함수
-(id)initWithItems:(NSArray*)items
offColor:(UIColor*)offcolor
onColor:(UIColor*)oncolor
offTextColor:(UIColor*)offtextcolor
onTextColor:(UIColor*)ontextcolor
fontSize:(int)fontsize
{
if (self = [super initWithItems:items]) {
//색 및 폰트크기 지정
offColor = [offcolor retain];
onColor = [oncolor retain];
offTextColor = [offtextcolor retain];
onTextColor = [ontextcolor retain];
fontSize = fontsize;
//스타일 고정
[self setBackgroundColor:[UIColor clearColor]];
[self setSegmentedControlStyle:UISegmentedControlStyleBar];
//루프를 돌면서 태그를 달아줌
id subview;
for (int i=0; i<[self.subviews count]; i++) {
subview = [self.subviews objectAtIndex:i];
[subview setTag:i];
}
//listen for updates
[self addTarget:self action:@selector(_setToggleHiliteColors) forControlEvents:UIControlEventValueChanged];
//비동기적으로 한번 호출해준다. 글자크기/배경색 적용을 위해...
[self performSelector:@selector(_setToggleHiliteColors) withObject:nil afterDelay:0.1];
}
return self;
}
//메모리 dealloc
-(void)dealloc {
[offColor release];offColor=nil;
[onColor release];onColor=nil;
[onTextColor release];onColor=nil;
[offTextColor release];offTextColor=nil;
[super dealloc];
}
@end
이제 만들어진 CustomSegmentedControl을 사용하는 예제 호스트 코드입니다. 복잡해 보이지만 IB에서 ToolBar 붙이고 거기에 UISegmentedControl을 중앙에 붙이기 위한 작업을 코드로 옮겨 넣은 것 뿐입니다.
UIToolbar *tb = [[[UIToolbar alloc]init]autorelease];
[tb setFrame:CGRectMake(0, 480-49, 320, 49)];
[tb setBarStyle:UIBarStyleBlack];
[window addSubview:tb];
CustomSegmentedControl *segControl;
segControl= [[CustomSegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"하나 ",@"둘 ",@"셋 ",nil]
offColor:[UIColor blackColor]
onColor:[UIColor colorWithRed:120.0f/255.0f green:120.0f/255.0f blue:120.0f/255.0f alpha:1]
offTextColor:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:152.0f/255.0f alpha:1]
onTextColor:[UIColor whiteColor]
fontSize:15];
segControl.frame = CGRectMake(0, 0, 250, 35);
segControl.selectedSegmentIndex=1;
//[segControl addTarget:self action:@selector(_valueChanged) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segControlItem = [[[UIBarButtonItem alloc]initWithCustomView:segControl]autorelease];
UIBarButtonItem *leftItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
UIBarButtonItem *rightItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
[tb setItems:[NSArray arrayWithObjects:leftItem, segControlItem, rightItem, nil]];
아래와 같이 선택했을때 색변경과 글자크기 변경이 가능해졌습니다.
좋은 지식이 되었으면 하며, 다른 의견 있으시면 댓글 부탁합니다. ^^
글쓴이 : 지돌스타(
http://blog.jidolstar.com)