This repository was archived by the owner on Nov 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTHNotesTextView.m
More file actions
186 lines (157 loc) · 6.86 KB
/
Copy pathTHNotesTextView.m
File metadata and controls
186 lines (157 loc) · 6.86 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// THNotesTextView.m
// THNotesTextViewExample
//
// Created by Hannes Tribus on 12/05/14.
// Copyright (c) 2014 3Bus. All rights reserved.
//
#import "THNotesTextView.h"
#define DEFAULT_VERTICAL_COLOR [UIColor colorWithRed:178.0/255.0 green:210.0/255.0 blue:241.0/255.0 alpha:1.0f]
#define DEFAULT_HORIZONTAL_COLOR [UIColor colorWithRed:255.0/255.0 green:214.0/255.0 blue:207.0/255.0 alpha:1.0f]
#define DEFAULT_MARGINS UIEdgeInsetsMake(10.0f, 20.0f, 0.0f, 20.0f)
@implementation THNotesTextView
@synthesize horizontalLineColor = _horizontalLineColor;
@synthesize verticalLineColor = _verticalLineColor;
@synthesize dottedLines = _dottedLines;
+ (void)initialize {
if (self == [THNotesTextView class])
{
id appearance = [self appearance];
[appearance setContentMode:UIViewContentModeRedraw];
[appearance setHorizontalLineColor:DEFAULT_HORIZONTAL_COLOR];
[appearance setVerticalLineColor:DEFAULT_VERTICAL_COLOR];
[appearance setMargins:DEFAULT_MARGINS];
}
}
#pragma mark - Setup/Teardown
- (void)setup {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
-(void)awakeFromNib {
[self setup];
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
#if !__has_feature(objc_arc)
[super dealloc];
#endif
}
- (void)setContentSize:(CGSize)contentSize {
contentSize = (CGSize) {
.width = contentSize.width - self.margins.left - self.margins.right,
.height = contentSize.height
};
NSLog(@"width: %f, height %f",contentSize.width,contentSize.height);
[super setContentSize:contentSize];
}
- (void)setMargins:(UIEdgeInsets)margins {
_margins = margins;
self.contentInset = (UIEdgeInsets) {
.top = self.margins.top,
.left = self.margins.left,
.bottom = self.margins.bottom,
.right = self.margins.right - self.margins.left
};
self.textContainerInset = (UIEdgeInsets) {
.top = self.margins.top,
.left = 0,
.bottom = self.margins.bottom,
.right = self.margins.right + self.margins.left
};
[self setContentSize:self.contentSize];
}
- (void)scrollToCaretAnimated:(BOOL)animated {
CGRect rect = [self caretRectForPosition:self.selectedTextRange.end];
rect.size.height += self.textContainerInset.bottom;
[self scrollRectToVisible:rect animated:animated];
}
- (UIColor *)horizontalLineColor {
if (!_horizontalLineColor) _horizontalLineColor = DEFAULT_HORIZONTAL_COLOR;
return _horizontalLineColor;
}
- (UIColor *)verticalLineColor {
if (!_verticalLineColor) _verticalLineColor = DEFAULT_VERTICAL_COLOR;
return _verticalLineColor;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lineSpacing = 1.01f;
if (self.horizontalLineColor) {
CGContextSetLineWidth(context, 1.0f);
if ([self isDottedLines]) {
CGFloat dash[] = {1.0, 1.0};
CGContextSetLineDash(context, 0.0, dash, 2);
}
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, self.horizontalLineColor.CGColor);
// Create un-mutated floats outside of the for loop.
// Reduces memory access.
CGFloat baseOffset = self.margins.top + self.font.descender + 1.0;
CGFloat screenScale = [UIScreen mainScreen].scale;
CGFloat boundsX = self.bounds.origin.x;
CGFloat boundsWidth = self.bounds.size.width;
// Only draw lines that are visible on the screen.
// (As opposed to throughout the entire view's contents)
NSInteger firstVisibleLine = MAX(1, (self.contentOffset.y / (self.font.lineHeight * lineSpacing)));
NSInteger lastVisibleLine = ceilf((self.contentOffset.y + self.bounds.size.height) / (self.font.lineHeight * lineSpacing));
for (NSInteger line = firstVisibleLine; line <= lastVisibleLine; ++line)
{
CGFloat linePointY = (baseOffset + (self.font.lineHeight * line * lineSpacing));
// Rounding the point to the nearest pixel.
// Greatly reduces drawing time.
CGFloat roundedLinePointY = roundf(linePointY * screenScale) / screenScale;
//NSLog(@"linePointYRounded %f (%f)",roundedLinePointY, linePointY);
CGContextMoveToPoint(context, boundsX, roundedLinePointY);
CGContextAddLineToPoint(context, boundsWidth, roundedLinePointY);
}
CGContextClosePath(context);
CGContextStrokePath(context);
}
if (self.verticalLineColor) {
if ([self isDottedLines]) {
CGFloat dash[] = {0.5, 1.5};
CGContextSetLineDash(context, 0.0, dash, 2);
}
// left
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, self.verticalLineColor.CGColor);
CGContextMoveToPoint(context, -1.0f, self.contentOffset.y);
CGContextAddLineToPoint(context, -1.0f, self.contentOffset.y + self.bounds.size.height);
CGContextClosePath(context);
CGContextStrokePath(context);
// right
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, self.verticalLineColor.CGColor);
CGContextMoveToPoint(context, -1.0f + self.contentSize.width, self.contentOffset.y);
CGContextAddLineToPoint(context, -1.0f + self.contentSize.width, self.contentOffset.y + self.bounds.size.height);
CGContextClosePath(context);
CGContextStrokePath(context);
}
}
#pragma mark - Notifications
- (void)textDidChangeNotification:(NSNotification*)notification {
[self scrollToCaretAnimated:NO];
}
- (void)keyboardWillHide:(NSNotification*)notification {
[self setMargins:DEFAULT_MARGINS];
}
- (void)keyboardDidChangeFrame:(NSNotification*)notification {
NSDictionary* keyboardInfo = [notification userInfo];
CGRect keyboardFrameBeginRect = [[keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
UIEdgeInsets tmp = DEFAULT_MARGINS;
tmp.bottom = [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight ?
keyboardFrameBeginRect.size.width : keyboardFrameBeginRect.size.height;
tmp.right = tmp.right - tmp.left;
[self setContentInset:tmp];
[self scrollToCaretAnimated:NO];
}
@end