/
BigLetterViewController.m
97 lines (76 loc) · 2.14 KB
/
BigLetterViewController.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
93
94
95
96
97
//
// BigLetterViewController.m
// SidebarAdvanced
//
// Created by BJ Homer on 5/3/12.
// Copyright (c) 2012 Instructure. All rights reserved.
//
#import "BigLetterViewController.h"
#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface BigLetterViewController () <UITextViewDelegate> {
__weak IBOutlet UITextView *textView;
}
@end
@implementation BigLetterViewController
- (id)init {
self = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:@"BigLetterViewController"];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
textView = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// We only want to allow a single letter.
if (text.length > 1) {
return NO;
}
if (text.length == 0) {
return YES;
}
// text must be of length == 1
if (textView.text.length == 0) {
return YES;
}
if (textView.text.length == 1 && range.length == 1) {
return YES;
}
return NO;
}
- (void)textViewDidChange:(UITextView *)textView {
[self updateSnapshot];
}
- (void)updateSnapshot {
UIImage *snapshot = [self snapshot];
RootViewController *root = (id)self.parentViewController;
[root useThumbnail:snapshot forChildController:self];
}
- (UIImage *)snapshot {
(void)[self view]; // Guarantee the view is loaded
CGSize size = CGSizeMake(80, 80);
CGSize startSize = textView.bounds.size;
CGFloat scaleX = size.width / startSize.width;
CGFloat scaleY = size.height / startSize.height;
CGFloat scale = (scaleX < scaleY ? scaleX : scaleY);
UIGraphicsBeginImageContextWithOptions(startSize, YES, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[textView.layer renderInContext:context];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshot;
}
@end