Text Scroll In iPhone

In this application we will see how to text file read from the resource and display in the scroll view. So let see how it will worked. My last post you can find out from here TextView .

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “Text_Scroll”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: In the Text_ScrollViewController.h class , make the following changes.

 

#import <uikit/UIKit.h>

@interface Text_ScrollViewController : UIViewController {

UITextView *textData;
UIScrollView *textScroll;

}

@property(nonatomic, retain) IBOutlet UITextView *textData;
@property(nonatomic, retain) IBOutlet UIScrollView *textScroll;

@end

 

 

Step 4 : Double click the Text_ScrollViewController.xib file and open it to the Interface Builder. First drag the Navigation Bar from the library and place it to the View window. Select the Navigation Bar and bring up Attribute Inspector, change the Title name into “Text Scroll”. Next drag the Scroll View from the library and place it to the view window. Connect File’s Owner icon to the view icon and select “textScroll”. Now save the .xib file and go back to the Xcode.

Step 5: Open the Text_ScrollViewController.m file and make the following changes.

 

#import "Text_ScrollViewController.h"

@implementation Text_ScrollViewController

@synthesize textData,textScroll;

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TextData" ofType:@"txt"];

if (filePath) {
NSString *myText1 = [NSString stringWithContentsOfFile:filePath];
if (myText1) {
//textView.text= myText;
textData= [[UITextView alloc] initWithFrame:CGRectMake(5,0, 316,350)]; //size.height-30 )];
textData.text = myText1;
[textData setFont:[UIFont systemFontOfSize:20]];
[textData setBackgroundColor:[UIColor clearColor]];
[textData setTextColor:[UIColor whiteColor]];
textData.editable = NO;
textData.dataDetectorTypes = UIDataDetectorTypeNone;

[textScroll addSubview:textData];

}
}

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

 

 

step 6: Now compile and run the application on the Simulator.

You can Download SourceCode from here Text_Scroll

Leave a Comment:

1 comment
Poornima says February 28, 2012

I hav the scroll view in which tableview is subview.
On the button click the records must be scrolled till its visible
How can I do this?

Thanks.

Reply
Add Your Reply