Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 13, 2011, 02:19 PM   #1
BrooklynDon
macrumors newbie
 
Join Date: Jan 2011
Scrolling Text - How to do it???

Hello folks!
I would like to scroll some text. I have it in a 280 X 31 label. The width will be variable and corresponds to a text file I have saved on our webpage thru a PHP script. TIA!
__________________
Don Stankalis
Web Developer - SUNY Downstate Medical Center
Programmer Since 1976
BrooklynDon is offline   0 Reply With Quote
Old Jan 13, 2011, 02:22 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Put the label in a UIScrollView. Hide the scroll bars. Let the label autosize to fit the content. Scroll the scrollview to scroll the text.
robbieduncan is offline   0 Reply With Quote
Old Jan 13, 2011, 09:45 PM   #3
adamk77
macrumors regular
 
Join Date: Jan 2008
Are you talking about a scrollable UILabel? Here is code that I use that works very well.

Code:
//
//  AutoScrollLabel.h
//  AutoScrollLabel
//
//  Created by Brian Stormont on 10/21/09.
//  Copyright 2009 Stormy Productions. All rights reserved.
//
//  Permission is granted to use this code free of charge for any project.
//

#import <UIKit/UIKit.h>

#define NUM_LABELS 2

enum AutoScrollDirection {
	AUTOSCROLL_SCROLL_RIGHT,
	AUTOSCROLL_SCROLL_LEFT,
};

@interface AutoScrollLabel : UIScrollView <UIScrollViewDelegate>{
	UILabel *label[NUM_LABELS];
	enum AutoScrollDirection scrollDirection;
	float scrollSpeed;
	NSTimeInterval pauseInterval;
	int bufferSpaceBetweenLabels;
	bool isScrolling;
}
@property(nonatomic) enum AutoScrollDirection scrollDirection;
@property(nonatomic) float scrollSpeed;
@property(nonatomic) NSTimeInterval pauseInterval;
@property(nonatomic) int bufferSpaceBetweenLabels;
// normal UILabel properties
@property(nonatomic,retain) UIColor *textColor;
@property(nonatomic, retain) UIFont *font;

- (void) readjustLabels;
- (void) setText: (NSString *) text;
- (NSString *) text;
- (void) scroll;


@end
Code:
//
//  AutoScrollLabel.m
//  AutoScrollLabel
//
//  Created by Brian Stormont on 10/21/09.
//  Copyright 2009 Stormy Productions. 
//
//  Permission is granted to use this code free of charge for any project.
//

#import "AutoScrollLabel.h"

#define LABEL_BUFFER_SPACE 50   // pixel buffer space between scrolling label
#define DEFAULT_PIXELS_PER_SECOND 100
#define DEFAULT_PAUSE_TIME 1.5f

@implementation AutoScrollLabel
@synthesize pauseInterval;
@synthesize bufferSpaceBetweenLabels;

- (void) commonInit
{
	for (int i=0; i< NUM_LABELS; ++i){
		label[i] = [[UILabel alloc] init];
		label[i].textColor = [UIColor whiteColor];
		label[i].backgroundColor = [UIColor clearColor];
		[self addSubview:label[i]];
	}
	
	scrollDirection = AUTOSCROLL_SCROLL_LEFT;
	scrollSpeed = DEFAULT_PIXELS_PER_SECOND;
	pauseInterval = DEFAULT_PAUSE_TIME;
	bufferSpaceBetweenLabels = LABEL_BUFFER_SPACE;
	self.showsVerticalScrollIndicator = NO;
	self.showsHorizontalScrollIndicator = NO;
	self.userInteractionEnabled = NO;
}

-(id) init
{
	if (self = [super init]){
        // Initialization code
		[self commonInit];
	}
	
	return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        // Initialization code
		[self commonInit];
    }
    return self;
	
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
		[self commonInit];
    }
    return self;
}


#if 0
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
	[NSThread sleepForTimeInterval:pauseInterval];

	isScrolling = NO;
	
	if ([finished intValue] == 1 && label[0].frame.size.width > self.frame.size.width){
		[self scroll];
	}	
}
#else
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
	isScrolling = NO;

	if ([finished intValue] == 1 && label[0].frame.size.width > self.frame.size.width){
		[NSTimer scheduledTimerWithTimeInterval:pauseInterval target:self selector:@selector(scroll) userInfo:nil repeats:NO];
	}
} 
#endif


- (void) scroll
{
	// Prevent multiple calls
	if (isScrolling){
//		return;
	}
	isScrolling = YES;
	
	if (scrollDirection == AUTOSCROLL_SCROLL_LEFT){
		self.contentOffset = CGPointMake(0,0);
	}else{
		self.contentOffset = CGPointMake(label[0].frame.size.width+LABEL_BUFFER_SPACE,0);
	}
	
	[UIView beginAnimations:@"scroll" context:nil];
    [UIView setAnimationDelegate:self];
	[UIView setAnimationCurve:UIViewAnimationCurveLinear];
	[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
	[UIView setAnimationDuration:label[0].frame.size.width/(float)scrollSpeed];
	
	if (scrollDirection == AUTOSCROLL_SCROLL_LEFT){
		self.contentOffset = CGPointMake(label[0].frame.size.width+LABEL_BUFFER_SPACE,0);
	}else{
		self.contentOffset = CGPointMake(0,0);
	}		
	
	[UIView commitAnimations];
}


- (void) readjustLabels
{
	float offset = 0.0f;
	
	for (int i = 0; i < NUM_LABELS; ++i){
		[label[i] sizeToFit];
		
		// Recenter label vertically within the scroll view
		CGPoint center;
		center = label[i].center;
		center.y = self.center.y - self.frame.origin.y;
		label[i].center = center;
		
		CGRect frame;
		frame = label[i].frame;
		frame.origin.x = offset;
		label[i].frame = frame;
		
		offset += label[i].frame.size.width + LABEL_BUFFER_SPACE;
	}
	
	CGSize size;
	size.width = label[0].frame.size.width + self.frame.size.width + LABEL_BUFFER_SPACE;
	size.height = self.frame.size.height;
	self.contentSize = size;

	[self setContentOffset:CGPointMake(0,0) animated:NO];
	
	// If the label is bigger than the space allocated, then it should scroll
	if (label[0].frame.size.width > self.frame.size.width){
		for (int i = 1; i < NUM_LABELS; ++i){
			label[i].hidden = NO;
		}
		[self scroll];
	}else{
		// Hide the other labels out of view
		for (int i = 1; i < NUM_LABELS; ++i){
			label[i].hidden = YES;
		}
		// Center this label
		CGPoint center;
		center = label[0].center;
		center.x = self.center.x - self.frame.origin.x;
		label[0].center = center;
	}

}


- (void) setText: (NSString *) text
{
	// If the text is identical, don't reset it, otherwise it causes scrolling jitter
	if ([text isEqualToString:label[0].text]){
		// But if it isn't scrolling, make it scroll
		// If the label is bigger than the space allocated, then it should scroll
		if (label[0].frame.size.width > self.frame.size.width){
			[self scroll];
		}
		return;
	}
	
	for (int i=0; i<NUM_LABELS; ++i){
		label[i].text = text;
	}
	[self readjustLabels];
}	
- (NSString *) text
{
	return label[0].text;
}


- (void) setTextColor:(UIColor *)color
{
	for (int i=0; i<NUM_LABELS; ++i){
		label[i].textColor = color;
	}
}

- (UIColor *) textColor
{
	return label[0].textColor;
}


- (void) setFont:(UIFont *)font
{
	for (int i=0; i<NUM_LABELS; ++i){
		label[i].font = font;
	}
	[self readjustLabels];
}

- (UIFont *) font
{
	return label[0].font;
}


- (void) setScrollSpeed: (float)speed
{
	scrollSpeed = speed;
	[self readjustLabels];
}

- (float) scrollSpeed
{
	return scrollSpeed;
}

- (void) setScrollDirection: (enum AutoScrollDirection)direction
{
	scrollDirection = direction;
	[self readjustLabels];
}

- (enum AutoScrollDirection) scrollDirection
{
	return scrollDirection;
}

- (void)dealloc {
	for (int i=0; i<NUM_LABELS; ++i){
		[label[i] release];
	}
    [super dealloc];
}


@end
__________________
http://www.uchidacoonga.com
"DO IT UNTIL IT BECOMES BORING, THEN KEEP DOING IT UNTIL IT BECOMES BEAUTIFUL."
adamk77 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
iPad synced with windows - just ordered a Mac - how to do it properly? snowydog iPad 3 Oct 29, 2011 06:58 AM
Family member giving me their free upgrade for White Iphone - How to do it?? VeNoMiZeD iPhone 1 Jun 1, 2011 04:08 PM
Tracing the movements a moving object on video - How to do it? dllavaneras Digital Video 4 Jan 1, 2009 10:41 PM
3D picture effect - How to do it? CrEsTo Digital Video 6 Jul 19, 2007 09:47 AM
Tiled page printing - How to do it? jive Mac Applications and Mac App Store 2 Jan 11, 2007 06:07 PM


All times are GMT -5. The time now is 07:49 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC