UIKit에 밑줄이 없어서 커스텀 버튼을 만들어 봤다.
tap시 전화연결 요청여부를 물어보고
누르고 있으면 복사하기 메뉴가 나타남.
//
#import <UIKit/UIKit.h>
@interface phoneButton : UIButton <UIAlertViewDelegate>
{
}
- (void)fireTouchUpInside;
@end
// phoneButton.m
#import "phoneButton.h"
@implementation phoneButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Tap Event
[self addTarget:self action:@selector(fireTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
// Long Press Event
UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPressGr];
[longPressGr release];
// title color
UIColor* fontColor = [UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:1.0];
[self setTitleColor:fontColor forState:UIControlStateNormal];
// Set Alignment
self.titleLabel.textAlignment = UITextAlignmentCenter;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 81.0/255.0, 102.0/255.0, 145.0/255.0, 1.0);
CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
CGFloat underLineWidth = textSize.width;
CGFloat underLineStart = 0.0;
CGFloat underLineEnd = 0.0;
switch (self.titleLabel.textAlignment)
{
default:
case UITextAlignmentCenter :
underLineStart = (self.frame.size.width - underLineWidth)/2;
break;
case UITextAlignmentLeft :
underLineStart = 0.0;
break;
case UITextAlignmentRight :
underLineStart = self.frame.size.width - underLineWidth;
break;
}
underLineEnd = underLineStart + underLineWidth;
CGContextMoveToPoint(context, underLineStart, rect.size.height);
CGContextAddLineToPoint(context, underLineEnd, rect.size.height);
CGContextStrokePath(context);
}
- (void)dealloc
{
[super dealloc];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void) longPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
NSLog(@"%@",[gestureRecognizer view]);
CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
UIMenuController *menuController = [UIMenuController sharedMenuController];
[self becomeFirstResponder];
[menuController setTargetRect
[menuController setMenuVisible
}
}
- (void)copy:
{
// called when copy clicked in menu
NSString* phoneNo = self.titleLabel.text;
[[UIPasteboard generalPasteboard] setString:phoneNo];
}
-
{
self.titleLabel.font = font;
}
-
{
NSString* phoneNo = self.titleLabel.text;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:phoneNo
message:@""
delegate:self
cancelButtonTitle:@"취소"
otherButtonTitles:@"통화", nil];
[alert show];
[alert release];
}
#pragma mark - AlertView Delgate
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex ==1)
{
NSString* callString = [NSString stringWithFormat:@"tel:%@", self.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callString]];
}
}
@end







