-
Notifications
You must be signed in to change notification settings - Fork 4
/
UIWebView+MADebugTools.m
92 lines (70 loc) · 3.54 KB
/
UIWebView+MADebugTools.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// UIWebView+MADebugTools.m
// http://github.com/michaelarmstrong
//
// Created by Michael Armstrong on 16/10/2014.
// Copyright (c) 2013 Michael Armstrong. All rights reserved.
//
// Just import this class into your project and add it into your Prefix.pch
// More features are coming later... I have a day job and a night job... so whenever theres time :)
// http://mike.kz / @italoarmstrong
//
#import "UIWebView+MADebugTools.h"
#import "MADebugTools.h"
@interface UIWebView()
@end
@implementation UIWebView (MADebugTools)
+ (void)load
{
Swizzle(self, @selector(awakeFromNib), @selector(override_awakeFromNib));
}
- (void)override_awakeFromNib
{
// run existing implementation
[self override_awakeFromNib];
#ifndef MA_DEBUG_TOOLS_DISABLE_WEB_DEBUG
// now run custom code
if([self viewWithTag:100] != nil && [self viewWithTag:101] != nil){
return;
}
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat consoleTextFieldHeight = 33.0f;
CGFloat consoleOutputTextViewHeight = (screenHeight * 0.3) > 100 ? (screenHeight * 0.3) : 100;
UITextField *consoleTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, screenWidth, consoleTextFieldHeight)];
consoleTextField.backgroundColor = [UIColor blackColor];
consoleTextField.alpha = 0.9f;
consoleTextField.textColor = [UIColor whiteColor];
UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 88, consoleTextFieldHeight)];
promptLabel.text = @" ~> #";
promptLabel.textColor = [UIColor whiteColor];
[consoleTextField setLeftViewMode:UITextFieldViewModeAlways];
[consoleTextField setLeftView:promptLabel];
consoleTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"enter javascript" attributes:@{NSForegroundColorAttributeName: [UIColor lightTextColor]}];
consoleTextField.font = [UIFont systemFontOfSize:11.0];
UIButton *evalButton = [[UIButton alloc] initWithFrame:CGRectMake(consoleTextField.bounds.size.width-88, 0, 88, consoleTextFieldHeight)];
[evalButton setTitle:@"eval" forState:UIControlStateNormal];
[evalButton setBackgroundColor:[UIColor redColor]];
[evalButton addTarget:self action:@selector(evalTapped:) forControlEvents:UIControlEventTouchUpInside];
[consoleTextField setRightView:evalButton];
[consoleTextField setRightViewMode:UITextFieldViewModeWhileEditing];
UITextView *consoleOutputTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, consoleTextField.frame.origin.y+consoleTextField.frame.size.height, screenWidth, consoleOutputTextViewHeight)];
consoleOutputTextView.backgroundColor = [UIColor darkGrayColor];
consoleOutputTextView.textColor = [UIColor whiteColor];
consoleOutputTextView.alpha = 0.8f;
consoleTextField.tag = 100;
consoleOutputTextView.tag = 101;
consoleOutputTextView.text = @"";
[self addSubview:consoleTextField];
[self addSubview:consoleOutputTextView];
#endif
}
- (void)evalTapped:(id)sender {
UITextView *consoleTextView = (UITextView *)[self viewWithTag:101];
UITextField *consoleTextField = (UITextField *)[self viewWithTag:100];
NSString *javascriptString = [NSString stringWithFormat:@"JSON.stringify(%@)",consoleTextField.text];
NSString *output = [self stringByEvaluatingJavaScriptFromString:javascriptString];
consoleTextView.text = [NSString stringWithFormat:@"%@\n%@",output,consoleTextView.text];
}
@end