2
2
// ColumnView.m
3
3
// Columns
4
4
//
5
- // Created by Rob Napier on 8/26/11.
6
- // Copyright (c) 2011 __MyCompanyName__. All rights reserved.
5
+ // Copyright (c) 2012 Rob Napier
6
+ //
7
+ // This code is licensed under the MIT License:
8
+ //
9
+ // Permission is hereby granted, free of charge, to any person obtaining a
10
+ // copy of this software and associated documentation files (the "Software"),
11
+ // to deal in the Software without restriction, including without limitation
12
+ // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
+ // and/or sell copies of the Software, and to permit persons to whom the
14
+ // Software is furnished to do so, subject to the following conditions:
15
+ //
16
+ // The above copyright notice and this permission notice shall be included in
17
+ // all copies or substantial portions of the Software.
18
+ //
19
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
22
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
+ // DEALINGS IN THE SOFTWARE.
7
26
//
8
27
9
28
#import " ColumnView.h"
10
29
#import < CoreText/CoreText.h>
11
30
12
31
static const CFIndex kColumnCount = 3 ;
13
32
14
- @interface CustomTextPosition : UITextPosition
15
- @property (nonatomic , readwrite , assign ) NSUInteger index;
16
- + (CustomTextPosition *)textPositionForIndex : (NSUInteger )index ;
17
- - (NSRange )rangeToPosition : (CustomTextPosition *)position ;
18
- @end
19
-
20
- @implementation CustomTextPosition
21
- + (CustomTextPosition *)textPositionForIndex : (NSUInteger )index {
22
- CustomTextPosition *position = [CustomTextPosition new ];
23
- position.index = index ;
24
- return position;
25
- }
26
-
27
- - (NSRange )rangeToPosition : (CustomTextPosition *)position {
28
- return NSMakeRange (self.index , position.index - position.index );
29
- }
30
- @end
31
-
32
- @interface CustomTextRange : UITextRange
33
- @property (nonatomic , readwrite , )
34
- - (NSRange )range ;
35
- @end
36
-
37
- @implementation CustomTextRange
38
- - (NSRange )range {
39
- return [(CustomTextPosition *)(self .start) rangeToPosition: (CustomTextPosition *)(self .end)];
40
- }
41
- @end
42
-
43
-
44
33
@interface ColumnView ()
45
- @property (nonatomic , readwrite , strong ) NSMutableAttributedString *storage;
46
34
@property (nonatomic , readwrite , assign ) CFIndex mode;
47
35
@end
48
36
@@ -164,39 +152,27 @@ - (id)initWithFrame:(CGRect)frame {
164
152
// Flip the view's context. Core Text runs bottom to top, even
165
153
// on iPad, and the view is much simpler if we do everything in
166
154
// Mac coordinates.
167
- // CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
168
- // CGAffineTransformTranslate(transform, 0, -self.bounds.size.height);
169
- // self.transform = transform;
155
+ CGAffineTransform transform = CGAffineTransformMakeScale (1 , -1 );
156
+ CGAffineTransformTranslate (transform, 0 , -self.bounds .size .height );
157
+ self.transform = transform;
170
158
self.backgroundColor = [UIColor whiteColor ];
171
159
}
172
160
return self;
173
161
}
174
162
175
- - (NSAttributedString *)attributedText {
176
- return [self .storage copy ];
177
- }
178
-
179
- - (void )setAttributedText : (NSAttributedString *)attributedText {
180
- _storage = [attributedText mutableCopy ];
181
- }
182
-
183
163
- (void )drawRect : (CGRect )rect
184
164
{
185
- if (self.storage == nil )
165
+ if (self.attributedString == nil )
186
166
{
187
167
return ;
188
168
}
189
169
170
+ // Initialize the context (always initialize your text matrix)
190
171
CGContextRef context = UIGraphicsGetCurrentContext ();
191
-
192
- // Flip the context (and always initialize your text matrix)
193
- CGContextSetTextMatrix ( context, CGAffineTransformIdentity );
194
- CGContextTranslateCTM ( context, rect.origin .x , rect.origin .y );
195
- CGContextScaleCTM ( context, 1 .0f , -1 .0f );
196
- CGContextTranslateCTM ( context, rect.origin .x , - ( rect.origin .y + rect.size .height ) );
197
-
172
+ CGContextSetTextMatrix (context, CGAffineTransformIdentity );
173
+
198
174
CFAttributedStringRef
199
- attrString = (__bridge CFTypeRef )self.storage ;
175
+ attrString = (__bridge CFTypeRef )self.attributedString ;
200
176
201
177
CTFramesetterRef
202
178
framesetter = CTFramesetterCreateWithAttributedString (attrString);
@@ -222,81 +198,9 @@ - (void)drawRect:(CGRect)rect
222
198
CFRelease (framesetter);
223
199
}
224
200
225
- // - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
226
- // self.mode = (self.mode + 1 ) % 4;
227
- // [self setNeedsDisplay];
228
- // }
229
-
230
- #pragma mark UITextInput
231
- - (NSString *)textInRange : (UITextRange *)range
232
- {
233
- return [self .storage.string substringWithRange: range.range];
234
- }
235
-
236
- - (void )replaceRange : (UITextRange *)range withText : (NSString *)text
237
- {
238
- [self .storage replaceCharactersInRange: range.range withString: text];
239
- }
240
-
241
- // @property (readwrite, copy) UITextRange *selectedTextRange;
242
- //
243
- // /* If text can be selected, it can be marked. Marked text represents provisionally
244
- // * inserted text that has yet to be confirmed by the user. It requires unique visual
245
- // * treatment in its display. If there is any marked text, the selection, whether a
246
- // * caret or an extended range, always resides witihin.
247
- // *
248
- // * Setting marked text either replaces the existing marked text or, if none is present,
249
- // * inserts it from the current selection. */
250
- //
251
- // @property (nonatomic, readonly) UITextRange *markedTextRange; // Nil if no marked text.
252
- // @property (nonatomic, copy) NSDictionary *markedTextStyle; // Describes how the marked text should be drawn.
253
-
254
- // - (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange; // selectedRange is a range within the markedText
255
- // - (void)unmarkText;
256
- //
257
- // /* The end and beginning of the the text document. */
258
- - (UITextPosition *)beginningOfDocument {
259
- return [CustomTextPosition textPositionForIndex: 0 ];
260
- }
261
-
262
- - (UITextPosition *)endOfDocument {
263
- return [CustomTextPosition textPositionForIndex: self .attributedText.length];
201
+ - (void )touchesEnded : (NSSet *)touches withEvent : (UIEvent *)event {
202
+ self.mode = (self.mode + 1 ) % 4 ;
203
+ [self setNeedsDisplay ];
264
204
}
265
205
266
- - (UITextRange *)textRangeFromPosition : (UITextPosition *)fromPosition toPosition : (UITextPosition *)toPosition {
267
- // - (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset;
268
- // - (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset;
269
- //
270
- // /* Simple evaluation of positions */
271
- // - (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other;
272
- // - (NSInteger)offsetFromPosition:(UITextPosition *)from toPosition:(UITextPosition *)toPosition;
273
- //
274
- // /* A system-provied input delegate is assigned when the system is interested in input changes. */
275
- // @property (nonatomic, assign) id <UITextInputDelegate> inputDelegate;
276
- //
277
- // /* A tokenizer must be provided to inform the text input system about text units of varying granularity. */
278
- // @property (nonatomic, readonly) id <UITextInputTokenizer> tokenizer;
279
- //
280
- // /* Layout questions. */
281
- // - (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction;
282
- // - (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction;
283
- //
284
- // /* Writing direction */
285
- // - (UITextWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction;
286
- // - (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(UITextRange *)range;
287
- //
288
- // /* Geometry used to provide, for example, a correction rect. */
289
- // - (CGRect)firstRectForRange:(UITextRange *)range;
290
- // - (CGRect)caretRectForPosition:(UITextPosition *)position;
291
- // - (NSArray *)selectionRectsForRange:(UITextRange *)range NS_AVAILABLE_IOS(6_0); // Returns an array of UITextSelectionRects
292
- //
293
- // /* Hit testing. */
294
- // - (UITextPosition *)closestPositionToPoint:(CGPoint)point;
295
- // - (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range;
296
- // - (UITextRange *)characterRangeAtPoint:(CGPoint)point;
297
- //
298
- //
299
- // @end
300
-
301
-
302
206
@end
0 commit comments