数字键盘,customKeyboard

一款非常实用的数字键盘送给大家,喜欢的换,可以给个哦


staticCGRectwldk_get_frame()

{

    CGFloat width = MIN([UIScreen mainScreen].bounds.size.width,

                        [UIScreen mainScreen].bounds.size.height);

    CGFloatheight = ((NSUInteger)(width * .32)) * 2;

    returnCGRectMake(0, 0, width, height);

}

staticUIImage*wldk_image(UIColor*color,CGSizesize)

{

    UIGraphicsBeginImageContext(size);


    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(ctx, color.CGColor);

    CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return image;

}

staticUIImage*wldk_delete_icon()

{

    CGFloat scale = [UIScreen mainScreen].scale;

    CGSizesize =CGSizeMake(27 * scale, 20 * scale);

    constCGFloatlineWidth = 1.f * scale;

    UIGraphicsBeginImageContext(size);


    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [[UIColor blackColor] setStroke];


    CGContextBeginPath(ctx);


    //// Bezier Drawing

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];

    [bezierPathmoveToPoint:CGPointMake(8.5 * scale, 19.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(23.15 * scale, 19.5 * scale)controlPoint1:CGPointMake(11.02 * scale, 19.5 * scale)controlPoint2:CGPointMake(20.63 * scale, 19.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(26.5 * scale, 15.5 * scale)controlPoint1:CGPointMake(25.66 * scale, 19.5 * scale)controlPoint2:CGPointMake(26.5 * scale, 17.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(26.5 * scale, 4.5 * scale)controlPoint1:CGPointMake(26.5 * scale, 13.5 * scale)controlPoint2:CGPointMake(26.5 * scale, 7.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(23.15 * scale, 0.5 * scale)controlPoint1:CGPointMake(26.5 * scale, 1.5 * scale)controlPoint2:CGPointMake(24.82 * scale, 0.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(8.5 * scale, 0.5 * scale)controlPoint1:CGPointMake(21.47 * scale, 0.5 * scale)controlPoint2:CGPointMake(11.02 * scale, 0.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(0.5 * scale, 9.5 * scale)controlPoint1:CGPointMake(5.98 * scale, 0.5 * scale)controlPoint2:CGPointMake(0.5 * scale, 9.5 * scale)];

    [bezierPathaddCurveToPoint:CGPointMake(8.5 * scale, 19.5 * scale)controlPoint1:CGPointMake(0.5 * scale, 9.5 * scale)controlPoint2:CGPointMake(5.98 * scale, 19.5 * scale)];

    [bezierPathclosePath];

    bezierPath.lineCapStyle = kCGLineCapRound;


    bezierPath.lineJoinStyle = kCGLineJoinRound;


    [UIColor.blackColor setStroke];

    bezierPath.lineWidth= lineWidth;

    [bezierPathstroke];



    //// Bezier 2 Drawing

    UIBezierPath* bezier2Path = [UIBezierPath bezierPath];

    [bezier2PathmoveToPoint:CGPointMake(19.5 * scale, 6.5 * scale)];

    [bezier2PathaddLineToPoint:CGPointMake(12.5 * scale, 13.5 * scale)];

    bezier2Path.lineCapStyle = kCGLineCapRound;


    bezier2Path.lineJoinStyle = kCGLineJoinRound;


    [UIColor.blackColor setStroke];

    bezier2Path.lineWidth= lineWidth;

    [bezier2Pathstroke];



    //// Bezier 3 Drawing

    UIBezierPath* bezier3Path = [UIBezierPath bezierPath];

    [bezier3PathmoveToPoint:CGPointMake(19.5 * scale, 13.5 * scale)];

    [bezier3PathaddLineToPoint:CGPointMake(12.5 * scale, 6.5 * scale)];

    bezier3Path.lineCapStyle = kCGLineCapRound;


    [UIColor.blackColor setStroke];

    bezier3Path.lineWidth= lineWidth;

    [bezier3Pathstroke];


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return [UIImage imageWithCGImage:image.CGImage

                               scale:scale

                         orientation:UIImageOrientationUp];

}

staticUIImage*wldk_keyboard_icon()

{

    CGFloat scale = [UIScreen mainScreen].scale;

    CGSizesize =CGSizeMake(38 * scale, 28 * scale);

    constCGFloatlineWidth = 2.f * scale;

    UIGraphicsBeginImageContext(size);


    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [[UIColor blackColor] setStroke];


    CGContextBeginPath(ctx);


    //// Rectangle Drawing

    UIBezierPath* rectanglePath = [UIBezierPathbezierPathWithRect:CGRectMake(1 * scale, 1 * scale, 36 * scale, 21 * scale)];

    [UIColor.blackColor setStroke];

    rectanglePath.lineWidth= lineWidth;

    [rectanglePathstroke];



    //// Bezier Drawing

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];

    [bezierPathmoveToPoint:CGPointMake(21.71 * scale, 25 * scale)];

    [bezierPathaddLineToPoint:CGPointMake(24 * scale, 25 * scale)];

    [bezierPathaddLineToPoint:CGPointMake(20 * scale, 28 * scale)];

    [bezierPathaddLineToPoint:CGPointMake(16 * scale, 25 * scale)];

    [bezierPathaddLineToPoint:CGPointMake(21.71 * scale, 25 * scale)];

    [bezierPathclosePath];

    [UIColor.blackColor setFill];

    [bezierPathfill];



    //// Rectangle 2 Drawing

    UIBezierPath* rectangle2Path = [UIBezierPathbezierPathWithRect:CGRectMake(5 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle2Pathfill];



    //// Rectangle 3 Drawing

    UIBezierPath* rectangle3Path = [UIBezierPathbezierPathWithRect:CGRectMake(10 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle3Pathfill];



    //// Rectangle 4 Drawing

    UIBezierPath* rectangle4Path = [UIBezierPathbezierPathWithRect:CGRectMake(15 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle4Pathfill];



    //// Rectangle 5 Drawing

    UIBezierPath* rectangle5Path = [UIBezierPathbezierPathWithRect:CGRectMake(20 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle5Pathfill];



    //// Rectangle 6 Drawing

    UIBezierPath* rectangle6Path = [UIBezierPathbezierPathWithRect:CGRectMake(25 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle6Pathfill];



    //// Rectangle 7 Drawing

    UIBezierPath* rectangle7Path = [UIBezierPathbezierPathWithRect:CGRectMake(30 * scale, 5 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle7Pathfill];



    //// Rectangle 8 Drawing

    UIBezierPath* rectangle8Path = [UIBezierPathbezierPathWithRect:CGRectMake(7 * scale, 10 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle8Pathfill];



    //// Rectangle 9 Drawing

    UIBezierPath* rectangle9Path = [UIBezierPathbezierPathWithRect:CGRectMake(12 * scale, 10 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle9Pathfill];



    //// Rectangle 10 Drawing

    UIBezierPath* rectangle10Path = [UIBezierPathbezierPathWithRect:CGRectMake(17 * scale, 10 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle10Pathfill];



    //// Rectangle 11 Drawing

    UIBezierPath* rectangle11Path = [UIBezierPathbezierPathWithRect:CGRectMake(22 * scale, 10 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle11Pathfill];



    //// Rectangle 12 Drawing

    UIBezierPath* rectangle12Path = [UIBezierPathbezierPathWithRect:CGRectMake(27 * scale, 10 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle12Pathfill];



    //// Rectangle 13 Drawing

    UIBezierPath* rectangle13Path = [UIBezierPathbezierPathWithRect:CGRectMake(6 * scale, 15 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle13Pathfill];



    //// Rectangle 14 Drawing

    UIBezierPath* rectangle14Path = [UIBezierPathbezierPathWithRect:CGRectMake(29 * scale, 15 * scale, 3 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle14Pathfill];



    //// Rectangle 15 Drawing

    UIBezierPath* rectangle15Path = [UIBezierPathbezierPathWithRect:CGRectMake(11 * scale, 15 * scale, 16 * scale, 3 * scale)];

    [UIColor.blackColor setFill];

    [rectangle15Pathfill];


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return [UIImage imageWithCGImage:image.CGImage

                               scale:scale

                         orientation:UIImageOrientationUp];

}

@interfaceUIView (WLDKFindFirstResponder)

+ (UIView*)wldk_firstResponder;

@end

@implementationUIView (WLDKFindFirstResponder)

+ (UIView*)wldk_firstResponder

{

    return [[[UIApplication sharedApplication] keyWindow] wldk_findFirstResponder];

}

- (UIView*)wldk_findFirstResponder

{

    if ([selfisFirstResponder]) return self;


    for (UIView*view in self.subviews) {

        UIView*responder = [viewwldk_findFirstResponder];

        if (responder) return responder;

    }


    return nil;

}

@end

@class WLDecimalKeyboardItem;

@interfaceWLDecimalKeyboardItem :UIButton

- (void)setBackgroundColor:(UIColor*)backgroundColor forState:(UIControlState)state;

@end

@implementationWLDecimalKeyboardItem

- (void)setBackgroundColor:(UIColor*)backgroundColor forState:(UIControlState)state

{

    [selfsetBackgroundImage:wldk_image(backgroundColor, CGSizeMake(1, 1))

                    forState:state];

}

@end

@interface WLDecimalKeyboard ()

{

    NSTimer*deleteTimer;

}

@property(weak,nonatomic)UIView *firstResponder;

@end

@implementationWLDecimalKeyboard

#pragma mark - Lifecycle

- (void)dealloc {

    [self_cleanTimer];

    NSLog(@"***> %s", __func__);

}

- (instancetype)initWithFrame:(CGRect)frame

{

    CGRect rect = wldk_get_frame();


    if (self = [superinitWithFrame:rect]) {

        [self_initUI];

    }


    return self;

}

- (instancetype)initWithTintColor:(UIColor*)tintColor

{

    CGRect rect = wldk_get_frame();


    if (self = [superinitWithFrame:rect]) {

        _tintColor= tintColor;

        [self_initUI];

    }


    return self;

}

#pragma mark - Private

- (void)_cleanTimer

{

    [deleteTimer invalidate];

    deleteTimer= nil;

    NSLog(@"***> %s", __func__);

}

- (void)_initUI

{

    self.backgroundColor = [UIColor colorWithRed:204 / 255.f

                                           green:204 / 255.f

                                            blue:204 / 255.f

                                           alpha:1.f];


    self.tintColor = self.tintColor ?: [UIColor colorWithRed:0 / 255.f

                                                       green:170 / 255.f

                                                        blue:238 / 255.f

                                                       alpha:1];


    /// 0(48), 1(49), 2(50), 3(51), 4(52), 5(53), 6(54), 7(55), 8(56), 9(57)

    /// .(46), del(127), cancel(-1), sure(-2)

    for (inti = 0; i < 14; ++i) {

        WLDecimalKeyboardItem *item = [WLDecimalKeyboardItem buttonWithType:UIButtonTypeCustom];

        if (i == 10) {

            item.tag= 46;

            [itemsetTitle:@"." forState:UIControlStateNormal];

        }

        else if (i == 11) {

            item.tag= 127;

            [itemsetImage:wldk_delete_icon() forState:UIControlStateNormal];

            UILongPressGestureRecognizer *g = [[UILongPressGestureRecognizer alloc] initWithTarget:self

                                                                                            action:@selector(deleteItemLongPress:)];

            [itemaddGestureRecognizer:g];

        }

        else if (i == 12) {

            item.tag= -1;

            item.userInteractionEnabled = NO;

//            [item setImage:wldk_keyboard_icon() forState:UIControlStateNormal];

        }

        else if (i == 13) {

            item.tag= -2;

            [itemsetTitle:@"确定" forState:UIControlStateNormal];

        }

        else {

            item.tag= 48 + i;

            [itemsetTitle:[NSString stringWithFormat:@"%d", i]

                  forState:UIControlStateNormal];

        }


        if (item.tag!= -2) {

            [itemsetBackgroundColor:[UIColor colorWithRed:250 / 255.f

                                                     green:250 / 255.f

                                                      blue:250 / 255.f

                                                     alpha:1]forState:UIControlStateNormal];

            [itemsetBackgroundColor:[UIColor colorWithRed:230 / 255.f

                                                     green:230 / 255.f

                                                      blue:230 / 255.f

                                                     alpha:1]forState:UIControlStateHighlighted];

            [itemsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            [item.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:25]];

        }

        else {

            CGFloath[1], s[1], b[1], a[1];

            [self.tintColorgetHue:h

                        saturation:s

                        brightness:b

                             alpha:a];


            UIColor*highlightColor = [UIColorcolorWithHue:h[0]

                                                 saturation:s[0]

                                                 brightness:b[0] - .1f

                                                      alpha:a[0]];


            [itemsetBackgroundColor:self.tintColor forState:UIControlStateNormal];

            [itemsetBackgroundColor:highlightColor forState:UIControlStateHighlighted];

            [itemsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];


        }


        [itemaddTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];


        [selfaddSubview:item];

    }


}

- (NSRange)_selectedRangeInInputView:(id)inputView

{

    UITextPosition* beginning = inputView.beginningOfDocument;


    UITextRange* selectedRange = inputView.selectedTextRange;

    UITextPosition* selectionStart = selectedRange.start;

    UITextPosition* selectionEnd = selectedRange.end;


    constNSIntegerlocation = [inputViewoffsetFromPosition:beginningtoPosition:selectionStart];

    constNSIntegerlength = [inputViewoffsetFromPosition:selectionStarttoPosition:selectionEnd];


    returnNSMakeRange(location, length);

}

- (void)_playClickAudio

{

    [[UIDevice currentDevice] playInputClick];

}

- (void)_insert:(NSString*)text

{

    if ([self.firstResponder isKindOfClass:[UITextField class]]) {



        id delegate = [(UITextField *)self.firstResponder delegate];


        UITextField *inputTF = (UITextField *)self.firstResponder;


//        NSLog(@"text    %@      %lu",text,inputTF.text.length);

            if ([delegaterespondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {

                NSRange range = [self _selectedRangeInInputView:(id)self.firstResponder];

                if ([delegatetextField:(UITextField *)self.firstResponder shouldChangeCharactersInRange:range replacementString:text]) {


                    if (inputTF.text.length== 0 && [text  isEqualToString:@"."]) {


                    }else{

                        if (![text  isEqualToString:@"."]) {

                            [self.firstResponderinsertText:text];

                        }else {

                            if(![inputTF.textcontainsString:@"."]){

                                [self.firstResponderinsertText:text];

                            }

                        }

                    }


                }

            } else {


                if (inputTF.text.length== 0 && [text  isEqualToString:@"."]) {


                }else{

                    if (![text  isEqualToString:@"."]) {

                        [self.firstResponderinsertText:text];

                    }else {

                        if(![inputTF.textcontainsString:@"."]){

                            [self.firstResponderinsertText:text];

                        }

                    }

                }

            }



    }

    else if ([self.firstResponder isKindOfClass:[UITextView class]]) {

        id delegate = [(UITextView *)self.firstResponder delegate];

        UITextView *inputTF = (UITextView *)self.firstResponder;

        if ([delegaterespondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {

            NSRange range = [self _selectedRangeInInputView:(id)self.firstResponder];

            if ([delegatetextView:(UITextView *)self.firstResponder shouldChangeTextInRange:range replacementText:text]) {

                if (inputTF.text.length== 0 && [text  isEqualToString:@"."]) {


                }else{

                    if (![text  isEqualToString:@"."]) {

                        [self.firstResponderinsertText:text];

                    }else {

                        if(![inputTF.textcontainsString:@"."]){

                            [self.firstResponderinsertText:text];

                        }

                    }

                }

            }

        } else {

            if (inputTF.text.length== 0 && [text  isEqualToString:@"."]) {


            }else{

                if (![text  isEqualToString:@"."]) {

                    [self.firstResponderinsertText:text];

                }else {

                    if(![inputTF.textcontainsString:@"."]){

                        [self.firstResponderinsertText:text];

                    }

                }

            }

        }

    }

    else {

        [self.firstResponder insertText:text];

    }

}

- (void)_clickedDot

{

    [self_insert:@"."];

}

- (void)_clickedDelete

{

    if ([self.firstResponder hasText]) {

        [self.firstResponder deleteBackward];

    }

}

- (void)_clickedDismiss

{

    [self.firstResponder resignFirstResponder];

}

- (void)_clickedSure

{

    [self.firstResponder resignFirstResponder];

    !self.done?: self.done();

}

- (void)_clickedDecimal:(int)decimal

{

    [self_insert:[NSString stringWithFormat:@"%d", decimal]];

}

- (void)_handleInputWithKeyboardItemTag:(NSUInteger)tag

{

    switch(tag) {

        case 46:

            [self_clickedDot];

            break;


        case 127:

            [self_clickedDelete];

            break;


        case -1:

            [self_clickedDismiss];

            break;


        case -2:

            [self_clickedSure];

            break;


        case 48:

        case 49:

        case 50:

        case 51:

        case 52:

        case 53:

        case 54:

        case 55:

        case 56:

        case 57:

            [self_clickedDecimal:(int)tag - 48];

            break;


        default:

            break;

    }

}

#pragma mark - Override

- (void)layoutSubviews

{

    [superlayoutSubviews];


    CGFloatwidth =CGRectGetWidth(self.bounds) / 4.f - .5f;

    CGFloatheight =CGRectGetHeight(self.bounds) / 4.f - .5f;


    [selfviewWithTag:49].frame=CGRectMake(0, .5, width, height);

    [selfviewWithTag:50].frame=CGRectMake(width + .5f, .5f, width, height);

    [selfviewWithTag:51].frame=CGRectMake((width + .5f) * 2, .5f, width, height);

    [selfviewWithTag:52].frame=CGRectMake(0, height + 1.f, width, height);

    [selfviewWithTag:53].frame=CGRectMake(width + .5f, height + 1.f, width, height);

    [selfviewWithTag:54].frame=CGRectMake((width + .5f) * 2, height + 1.f, width, height);

    [selfviewWithTag:55].frame=CGRectMake(0, .5f + (height + .5f) * 2, width, height);

    [selfviewWithTag:56].frame=CGRectMake(width + .5f, .5f + (height + .5f) * 2, width, height);

    [selfviewWithTag:57].frame=CGRectMake((width + .5f) * 2, .5f + (height + .5f) * 2, width, height);

    [selfviewWithTag:48].frame=CGRectMake(width + .5f, .5f + (height + .5f) * 3, width, height);

    [selfviewWithTag:46].frame=CGRectMake(0, .5f + (height + .5f) * 3, width, height);

    [selfviewWithTag:-1].frame=CGRectMake((width + .5f) * 2, .5f + (height + .5f) * 3, width, height);

    [selfviewWithTag:127].frame=CGRectMake((width + .5f) * 3, .5f, width, height * 2 + .5f);

    [selfviewWithTag:-2].frame=CGRectMake((width + .5f) * 3, 1.f + height * 2, width, height * 2 + 1.f);

}

#pragma mark - Action

- (void)buttonTouchUpInside:(WLDecimalKeyboardItem*)sender

{

    if (!self.firstResponder || ![self.firstResponder isFirstResponder]) {

        self.firstResponder = (UIView*)[UIView wldk_firstResponder];

        if (!self.firstResponder) return;

        if (![self.firstResponder conformsToProtocol:@protocol(UIKeyInput)]) return;

    }


    [self_playClickAudio];

    [self_handleInputWithKeyboardItemTag:sender.tag];

}

- (void)deleteItemLongPress:(UILongPressGestureRecognizer*)longPress

{

    if (longPress.state == UIGestureRecognizerStateBegan) {

        dispatch_async(dispatch_get_main_queue(), ^{

            ((UIButton*)[selfviewWithTag:127]).highlighted=YES;

        });

        deleteTimer = [NSTimer scheduledTimerWithTimeInterval:.1

                                                       target:self

                                                     selector:@selector(_repeatLongPressDelete)

                                                     userInfo:nil

                                                      repeats:YES];

    }

    else if (longPress.state == UIGestureRecognizerStateEnded

             || longPress.state == UIGestureRecognizerStateCancelled

             || longPress.state == UIGestureRecognizerStateFailed) {

        dispatch_async(dispatch_get_main_queue(), ^{

            ((UIButton*)[selfviewWithTag:127]).highlighted=NO;

        });

        [self_cleanTimer];

    }

}

- (void)_repeatLongPressDelete

{

    [(UIControl *)[self viewWithTag:127] sendActionsForControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - UIInputViewAudioFeedback

- (BOOL)enableInputClicksWhenVisible

{

    returnYES;

}

希望大家会喜欢

Demo地址 https://github.com/1461139257/shuziJianPan.git

你可能感兴趣的:(数字键盘,customKeyboard)