-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFBScrollingTextView.m
116 lines (93 loc) · 2.86 KB
/
FBScrollingTextView.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
//
// FBScrollingTextView.m
// FBScrollingTextView
//
// Created by Fábio Bernardo on 2/6/11.
//
#import "FBScrollingTextView.h"
#define kFBScrollingTextViewSpacing 0.5
#define kFBScrollingTextViewDefaultScrollingSpeed 2
#define kFBScrollingTextViewStartScrollingDelay 0.3
@implementation FBScrollingTextView
@synthesize scrollingSpeed;
@synthesize string;
@synthesize font;
- (void)scrollText {
cursor.x-=1;
[self setNeedsDisplay:YES];
}
- (void)startScrolling {
if (!tickTockScroll) {
tickTockScroll = [NSTimer scheduledTimerWithTimeInterval:refreshRate/scrollingSpeed target:self selector:@selector(scrollText) userInfo:nil repeats:YES];
}
[tickTockStartScrolling release];
tickTockStartScrolling = nil;
}
- (CGFloat)stringWidth {
if (!string) return 0;
NSSize stringSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil]];
return stringSize.width;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
scrollingSpeed = kFBScrollingTextViewDefaultScrollingSpeed;
refreshRate = 0.05;
cursor = NSMakePoint(0, 0);
self.font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
}
return self;
}
- (void)setString:(NSString *)_string {
if (tickTockScroll) {
[tickTockScroll invalidate];
[tickTockScroll release];
tickTockScroll = nil;
}
if (tickTockStartScrolling) {
[tickTockStartScrolling invalidate];
[tickTockStartScrolling release];
tickTockStartScrolling = nil;
}
cursor = NSMakePoint(0, 0);
[string release];
string = [_string retain];
CGRect thisFrame = [super frame];
if ([self stringWidth] > thisFrame.size.width) {
if (!tickTockStartScrolling) {
tickTockStartScrolling = [NSTimer scheduledTimerWithTimeInterval:kFBScrollingTextViewStartScrollingDelay target:self selector:@selector(startScrolling) userInfo:nil repeats:NO];
}
}
[self setNeedsDisplay:YES];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
// Drawing code.
CGFloat sWidth = round([self stringWidth]);
CGFloat rWidth = round(rect.size.width);
CGFloat spacing = round(rWidth*kFBScrollingTextViewSpacing);
if ((cursor.x*-1) == sWidth) {
CGFloat diff = spacing - (sWidth+cursor.x);
cursor.x = rWidth-diff;
}
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
[string drawAtPoint:cursor withAttributes:attrs];
CGFloat diff = spacing - (sWidth+cursor.x);
if (diff >= 0) {
NSPoint point = NSMakePoint(rWidth-diff, cursor.y);
[string drawAtPoint:point withAttributes:attrs];
}
}
- (void)dealloc {
[tickTockScroll invalidate];
[tickTockScroll release];
tickTockScroll = nil;
[tickTockStartScrolling invalidate];
[tickTockStartScrolling release];
tickTockStartScrolling = nil;
self.string = nil;
self.font = nil;
[super dealloc];
}
@end