/
JJTextView.m
135 lines (111 loc) · 4.52 KB
/
JJTextView.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
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
//
// JJTextView.m
// iTextus
//
// Created by Jiang Jiang on 1/29/10.
// Copyright 2010 Jjgod Jiang. All rights reserved.
//
#import "JJTextView.h"
#import "DetailViewController.h"
@implementation JJTextView
@synthesize controller;
- (void) touchesEnded: (NSSet *) touches
withEvent: (UIEvent *) event
{
JJBook *book = controller.detailItem;
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInView: self];
if (location.y < 256)
{
if (book.lastReadPage > 0)
{
book.lastReadPage -= 1;
[controller hideAll];
[self setNeedsDisplay];
}
}
else if (location.y > 768)
{
book.lastReadPage += 1;
[controller hideAll];
[self setNeedsDisplay];
}
else
[controller toggleAll];
// NSLog(@"touched: %g, %g", location.x, location.y);
}
}
#define ARRSIZE(a) (sizeof(a) / sizeof(a[0]))
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect
{
JJBook *book = controller.detailItem;
// NSLog(@"drawRect: %@", NSStringFromCGRect(rect));
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *tile = [UIImage imageNamed: @"brushed_alu.png"];
UIColor *color = [UIColor colorWithPatternImage: tile];
[color setFill];
// CGContextSetRGBFillColor(context, 1.0, 1.0, 0.85, 1.0);
CGContextFillRect(context, rect);
CGContextSetAllowsFontSmoothing(context, true);
CGContextSetAllowsFontSubpixelPositioning(context, true);
CGContextSetShouldSmoothFonts(context, true);
CGContextSetShouldSubpixelPositionFonts(context, true);
if (! book)
return;
if (! textAttributes)
{
NSLog(@"Preparing text attributes");
CTFontRef font = CTFontCreateWithName(CFSTR("FZShuSong-Z01"), 24.0, NULL);
CGFloat paragraphSpacing = 4.0;
CGFloat lineSpacing = 8.0;
CTParagraphStyleSetting settings[] = {
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing },
{ kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing },
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, ARRSIZE(settings));
textAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
(id) font, (NSString *) kCTFontAttributeName,
(id) paragraphStyle, (NSString *) kCTParagraphStyleAttributeName, nil];
CFRelease(font);
}
CGSize inset = CGSizeMake(70, 80);
CGRect pageFrame = CGRectMake(inset.width, inset.height,
rect.size.width - 2 * inset.width,
rect.size.height - 2 * inset.height);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
NSDate *currentTime = [NSDate date];
JJPage *page = [book loadPage: book.lastReadPage
withAttributes: textAttributes
frame: pageFrame];
NSTimeInterval interval = -[currentTime timeIntervalSinceNow];
if (page)
{
// NSLog(@"Start drawing page %d", book.lastReadPage);
CGContextSaveGState(context);
CGContextConcatCTM(context, CGAffineTransformMakeScale(1, -1));
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(0, -(pageFrame.origin.y * 2 + 20 + pageFrame.size.height)));
CGRect lineBounds = CTLineGetImageBounds(book.titleLine, context);
CGContextSetTextPosition(context, (rect.size.width - lineBounds.size.width) / 2 - 10, pageFrame.origin.y + pageFrame.size.height + 35);
CTLineDraw(book.titleLine, context);
CTFrameDraw(page.textFrame, context);
CGContextRestoreGState(context);
} else {
book.lastReadPage = book.pages.count - 1;
}
NSLog(@"used %g secs", interval);
// NSLog(@"estimatedPages = %d, pages = %d", book.estimatedPages, book.pages.count);
CGFloat seenWidth = rect.size.width * (book.lastReadPage + 1) / book.estimatedPages;
CGContextSetRGBFillColor(context, 0.7, 0.7, 0.7, 1.0);
CGContextFillRect(context, CGRectMake(0, rect.size.height - 5, seenWidth, 5));
CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 1.0);
CGContextFillRect(context, CGRectMake(seenWidth, rect.size.height - 5, rect.size.width - seenWidth, 5));
}
- (void) dealloc
{
[textAttributes release];
[super dealloc];
}
@end