Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
173 lines (140 sloc) 4.21 KB
//
// CustomTextScrollView.m
// ScrollingTextSample
//
// Created by maicki on 23.11.10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CustomTextScrollView.h"
#import <Quartz/Quartz.h>
@interface CustomTextScrollView (Color)
- (CGColorRef)black;
- (CGColorRef)white;
- (CGColorRef)blue;
- (CGColorRef)red;
- (CGColorRef)green;
@end
@implementation CustomTextScrollView (Color)
- (CGColorSpaceRef)genericRGBSpace
{
static CGColorSpaceRef space = NULL;
if (NULL == space) {
space = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);
}
return space;
}
- (CGColorRef)black
{
static CGColorRef black = NULL;
if (black == NULL) {
CGFloat values[4] = {0.0, 0.0, 0.0, 1.0};
black = CGColorCreate([self genericRGBSpace], values);
}
return black;
}
- (CGColorRef)white
{
static CGColorRef white = NULL;
if (white == NULL) {
CGFloat values[4] = {1.0, 1.0, 1.0, 1.0};
white = CGColorCreate([self genericRGBSpace], values);
}
return white;
}
- (CGColorRef)blue
{
static CGColorRef blue = NULL;
if (blue == NULL) {
CGFloat values[4] = {0.0, 0.0, 1.0, 1.0};
blue = CGColorCreate([self genericRGBSpace], values);
}
return blue;
}
- (CGColorRef)red
{
static CGColorRef red = NULL;
if (red == NULL) {
CGFloat values[4] = {1.0, 0.0, 0.0, 1.0};
red = CGColorCreate([self genericRGBSpace], values);
}
return red;
}
- (CGColorRef)green
{
static CGColorRef green = NULL;
if (green == NULL) {
CGFloat values[4] = {0.0, 1.0, 0.0, 1.0};
green = CGColorCreate([self genericRGBSpace], values);
}
return green;
}
@end
#pragma mark - CustomTextScrollView
@interface CustomTextScrollView ()
@property (nonatomic, readwrite, strong) CAScrollLayer *scrollLayer;
@property (nonatomic, readwrite, strong) CATextLayer *textLayer;
@property (nonatomic, assign) CGFloat fontSize;
@end
@implementation CustomTextScrollView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_fontSize = 14;
// ScrollLayer
_scrollLayer = [CAScrollLayer layer];
_scrollLayer.frame = NSRectToCGRect(self.bounds);
// TextLayer
_textLayer = [CATextLayer layer];
_textLayer.foregroundColor = [self black];
_textLayer.fontSize = _fontSize;
// Set textLayer as sublayer of the scrollLayer
[_scrollLayer addSublayer:_textLayer];
[self setLayer:_scrollLayer];
}
return self;
}
- (void)setText:(NSString *)text
{
[self.textLayer removeFromSuperlayer];
self.textLayer.string = text;
// Get the width of the text and set it as the new width of the textLayer
NSDictionary *attributes = @{
NSFontAttributeName : [NSFont fontWithName:@"Helvetica" size:self.textLayer.fontSize]
};
CGFloat textWidth = [text sizeWithAttributes:attributes].width;
NSRect textRect = NSMakeRect(0.f, 0.f, textWidth, CGRectGetHeight(self.scrollLayer.frame));
self.textLayer.frame = NSRectToCGRect(textRect);
// Animate only if text is longer than the view
if (self.frame.size.width < textRect.size.width) {
[self addAnimation];
}
else {
// Place text in the middle of the textlayer
CGFloat newX = floor((self.frame.size.width - textRect.size.width) / 2.f);
NSRect newTextRect = NSMakeRect(newX, 0.f, CGRectGetWidth(self.textLayer.frame), CGRectGetHeight(self.textLayer.frame));
self.textLayer.frame = NSRectToCGRect(newTextRect);
}
[self.scrollLayer addSublayer:self.textLayer];
}
- (void)addAnimation
{
CGFloat rightLeftPadding = 10.f;
// Prepare the animation from the current position to the new position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 2.5;
animation.repeatCount = HUGE_VALF;
// Left edge
NSPoint from = self.textLayer.position;
from.x += rightLeftPadding;
animation.fromValue = [NSValue valueWithPoint:from];
// Right edge
NSPoint to = self.textLayer.position;
to.x -= (CGRectGetWidth(self.textLayer.frame) - CGRectGetWidth(self.frame) + rightLeftPadding);
animation.toValue = [NSValue valueWithPoint:to];
// Add the animation, overriding the implicit animation.
[self.textLayer addAnimation:animation forKey:@"position"];
}
@end
Something went wrong with that request. Please try again.