UITextView not word wrapping - iphone

I'm new to IOS development, and I'm just trying to get my head around all the views. At first I have a UITextField which is one line and I wanted it to wrap. I was told to implement a UITextView instead - and that worked! I then had many lines of wrapped text.
The next thing I wanted to do was have horizontally scrolling pages. I downloaded apple's page control example. In it many pages are created with a little text label. I altered this to contain a very long string, and replaced the UILabel type with a UITextView. However, it's not word wrapping! The cause could be anything an I have absolutely no idea what it might be. Perhaps it's inheriting some setting from something?
Any help would be much appreciated.
Edit - here is where the views are created:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
NSDictionary *numberItem = [self.contentList objectAtIndex:page];
controller.numberTitle.text = [numberItem valueForKey:NameKey];
}
}
and this is the MyViewController header:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController
{
UILabel *pageNumberLabel;
int pageNumber;
UITextView *numberTitle;
UIImageView *numberImage;
}
#property (nonatomic, retain) IBOutlet UILabel *pageNumberLabel;
#property (nonatomic, retain) IBOutlet UITextView *numberTitle;
#property (nonatomic, retain) IBOutlet UIImageView *numberImage;
- (id)initWithPageNumber:(int)page;
#end

Check the frame of the UITextView. It's possible if you just swapped UITextView with UILabel that the frame is not large enough. Change the frame to have a larger height. Also, set the background color to a bright color so you know can see how large it is on the screen.

Even though everything in the program was changed, the xib file still contained a UILabel which is what was displaying the text.

Related

How to load bundle of images in a scrollview with horrizondal scrolling in iphone?

I have an iPhone application in which i need to load a bulk of images (ie around 50),which has a small rect like (little above thumbnail) containing 3 in each row with 4 columns in one page, and i need to scroll horizontally to load the next 12 images and so on. Also i need to show the page number under it as 1,2 etc.
Can any body guide me in the right direction to achieve this in a scroll view ?i am really in a mess ,how to add imageview to a scrollview like this ,also each image view has its own buttons also.
The way i would tackle this problem goes as follows: (I am not going to provide many details as they are dependent on your particular needs and requirements).
.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIScrollViewDelegate>
#property (unsafe_unretained, nonatomic) IBOutlet UIScrollView *scrollView;//assuming the scrollView is connected in a xib file
-(id)initWithImages:(NSArray*)images;//that could be whatever format you get your images(UIImage, urls to download, file names or custom views. Here we assume that the array contains instances of UIImage)
#end
.m file:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSArray *images;
#property (nonatomic, assign) CGFloat pageWidth;
#property (nonatomic, assign) int currentPage;
#end
#implementation ViewController
-(id)initWithImages:(NSArray*)images{
self = [super init];
if(self){
self.images = images;
}
return self;
}
-(void)viewDidLoad{
[super viewDidLoad];
[self.scrollView setDelegate:self];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.imageURLsArray.count,
self.scrollView.frame.size.height)];
[self.scrollView setPagingEnabled:YES];
[self.scrollView setShowsVerticalScrollIndicator:NO];
self.pageWidth = self.scrollView.frame.size.width;
for(int i = 0; i < self.images.count; i++){
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width*i,
self.scrollView.frame.origin.y,
self.scrollView.frame.size.width,
self.scrollView.frame.size.height)];
imageView.image = [self.images objectAtIndex:i];
[self.scrollView addSubview:imageView];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = floor(scrollView.contentOffset.x - self.pageWidth/2)/self.pageWidth + 1;
if(self.currentPage != page){
NSLog(#"Scrolled to new page: %d", page);
self.currentPage = page;
//do additional things based on the fact that you have scrolled to a new page (like changing the page number)
}
}
#end
Use UItableview create custom cell with two imageviews on it and load that custom cell to cell for indexpath then create an nsarray and pass the image names into that array and then convert that image into data format for eg
nsdata *data = [NSdata dataWithcontents of url [array object at index:indexpath.row]];
cell.imageview.image = [uiimage imagewithdata:data];
for this you should know to create custom cells.

Adding UIPanGestureRecognizer to newly created UIView, and replace current view with slide

I have read almost every post I can find on this site. I have found some extremely useful posts, but have not quite found how to solve my problem. I am trying to create a springboardish image viewer, were a user can slide the next image into place, but stop halfway though and go back. I have read on numerous posts that a PanGestureRecognizer is the way to go, but have been having a very difficult time with it.
In Storyboard, I created my ViewController and added a PanGestureRecognizer to the ViewController and added an action.
Here is my .h:
#interface PanViewController : UIViewController
- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer;
#end
Here is my .m:
#interface PanViewController ()
#property (nonatomic, retain) NSArray *PhotoBundle;
#property (nonatomic, retain) NSMutableArray *PhotoArray;
#property (nonatomic, retain) NSMutableArray *ImgViewArray;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//set i to the correct range if needed.
if (i > 0 && 1 <= _PhotoArray.count) {
}
else i = 0;
//create photo bundle from directory
_PhotoBundle = [[NSBundle mainBundle] pathsForResourcesOfType:#".jpg"inDirectory:#"Otter_Images"];
//create array from bundle of paths.
_PhotoArray = [[NSMutableArray alloc] initWithCapacity:_PhotoBundle.count];
for (NSString* path in _PhotoBundle)
{
[_PhotoArray addObject:[UIImage imageWithContentsOfFile:path]];
}
//create initial image view
UIImage *currentImage = [[UIImage alloc] init];
currentImage = [_PhotoArray objectAtIndex:i];
UIImageView *currentIV = [[UIImageView alloc]initWithFrame:CGRectMake(100,100, 100, 100)];
[currentIV setImage:currentImage];
[self.view addSubview:currentIV];
//increment i to progress through array.
i++;
}
- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer {
[self GestureDirection:recognizer];
}
- (void)GestureDirection:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint direction = [gestureRecognizer velocityInView:self.view];
if(direction.x > 0)
{
NSLog(#"gesture went right");
UIImage *nextImg = [[UIImage alloc] init];
nextImg = [_PhotoArray objectAtIndex:i];
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
UIImageView *nextIV = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
[nextIV setImage:nextImg];
[newView addSubview:nextIV];
self.view = newView;
}
else
{
NSLog(#"gesture went left");
}
}
It shows the next image on the pan, but then there is no PanGestureRecongizer on the new View that is loaded in. I thought that when I added the PANGesture in Storyboard, it would make it available to all views in the ViewController.
This may be a separate question, but how do I get the animation to work on the PanGesture? I was incorrectly (obviously) under the impression that a Pan would do the slide in effect.
Thank you all for your help!!
So like Tom Irving said, I used a UIScrollView with paging. I did play around with creating a new gesture recognizer each time and adding paging, but it was really complicated and I was never able to get it to work.

IBOutlet UIImageView

I got 30 of these codes, with the same implementation:
// .h
#interface ViewController : UIViewController{
IBOutlet UIImageView *circle;
IBOutlet UIImageView *circle2;
}
#property (nonatomic, retain) IBOutlet UIImageView *circle;
#property (nonatomic, retain) IBOutlet UIImageView *circle2;
// .m
#implementation ViewController
#synthesize circle;
#synthesize circle2;
- (void)viewDidLoad
{
circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"Circle.png"]];
circle2 = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"Circle.png"]];
}
And somewhere in my code, Im adding it as a subview.
My problem is,Is there a way to make it shorter, for it to be maintainable.
You can use one IBOutletCollection instead of 30 IBOutlets. You probably want to set the tag on each UIImageView though, so you can still tell them apart.
(This answer assumes you use a nib. Remove the lines where you instantiate the UIImageViews in viewDidLoad if so.)
If you use name like - Circle1.png, Circle2.png, then you can go for for loop for creating this in a loop.
Something like -
for (int i = 0; i < imageCount ; i++ ) {
circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[NSString stringWithFormat: #"Circle%d.png" , i]]];
}
Is there a pattern to where you are putting these views in their superview? If so, you could use a for loop that goes to 30 and programmatically create the views and add them to their superview.
So for example:
for (i = 0; i < 100; i++)
{
UIImageView* circle = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"Circle.png"]];
[self.view addSubview:circle];
}
Would add 100 of the image views you would want. Of course you will need to adjust the positions of each of these views in the loop, but this is the general idea.
while creating this viewcontroller pass the frame as a parameter.thats the only thing changing rite??.the image is same and as there are no other properties .. it may work

iphone. making UITextView but won't make it visible

#interface SomeViewController : UIViewController <UITextViewDelegate>
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) UIImage *image;
#property (nonatomic, retain) IBOutlet UITextView *textView;
I need textview(editable) in my view controller.
so I set protocol ,
and also, IB to the file's owner, too. (I don't know, is it unnecessary?)
And my .m file.... viewDidLoad..
_textView = [[UITextView alloc]init];
[_textView setFrame:CGRectMake(8, 110, 304, 82)];
[_textView setFont:[UIFont boldSystemFontOfSize:12]];
_textView.editable = YES;
//[textView setText:#"hdgffgsfgsfgds"];// if uncommented, this actually visible...
[self.view insertSubview:_textView atIndex:2];
So far, it's working. But I want these methods to run...
- (void)textViewDidChange:(UITextView *)textView
{
if([_textView.text length] == 0)
{
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
}
}
But after build, my textview is still empty.
What did I wrong?
You need to add the delegate to your textview, try with this:
_textView.delegate = self;
On the other hand,
textViewDidChange
Will only work when you change your text, if you want to place a text in the textview, you can do:
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
In your ViewdidLoad
When synthesizing the textView, do you do it like #synthesize textView = _textView so that you can use the underscored name? Usually the underscored one is used only in the setter/getter methods (for instance when overriding them).
Note that
The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.
This is from Apple's documentation for the method you are using. This means if you are changing the text through the code, the method will not get fired.
Are you expecting [textView setText:#"hdgffgsfgsfgds"] to in turn invoke textViewDidChange:? Calling setText will not automatically invoke the textViewDidChange:.

iOS: Can I have a UITableView scrolling horizontally in the iPhone landscape mode ? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Horizontal scrolling UITableView
Can I have a UITableView scrolling horizontally in the iPhone landscape mode ?
This is how it looks like now: http://cl.ly/95xZ
This is how it should be: http://cl.ly/93IY
I was wondering if I should use a UIScrollView for the landscape mode and therefore to switch between UITableView and UIScrollView depending on the orientation of the device...
thanks
You can try this
HorizontalTable for iOS
What you need is UIScrollView with pagination. It works essentially the same way a standard UITable does. When you scroll from left to right you just push new "cells" on the stack and present them.
Here is a sample code:
// .h File
#interface PagingViewController : UIViewController <UIScrollViewDelegate>{
NSMutableArray *cells;
}
#property(nonatomic,retain)NSMutableArray *cells;
-(id)initWithCells:(NSMutableArray*)tableCells;
-(void)loadPage:(int)page;
#end
// .m File
#interface PagingViewController ()
#property(nonatomic,retain)NSMutableArray *viewControllers;
#property(nonatomic,retain)UIView *contentView;
#property(nonatomic,readwrite)NSUInteger numberOfPages;
#end
#implementation PagingViewController
#synthesize viewControllers;
#synthesize contentView;
#synthesize cells;
#synthesize numberOfPages;
-(id)initWithCells:(NSMutableArray*)tableCells{
self = [super init];
if(self) {
self.cells = tableCells;
numberOfPages = [cells count];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < numberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = contentView;
[self loadPage:0];
}
return self;
}
- (void)loadPage:(int)page{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
else if(page > 0){
CustomCellVC *cell = [viewControllers objectAtIndex:page];
if ((NSNull *)cell == [NSNull null]){
cell = (CustomCellVC*)[cells objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:cell;
[cell release];
}
if (cell.view.superview == nil)
{
[contentView addSubview:cell.view];
}
}
}
//Dont forget to dealloc.
I wrote this from memory so there might be little bugz.
To implement horizontal scrolling you should use UIScrollView and manage its subviews manually. It's not very hard as it sounds. There are several videos from WWDC about technics which you can use to work with scroll view. They are worth watching.
Advanced ScrollView Techniques at
https://developer.apple.com/videos/play/wwdc2011/104/
Designing Apps with Scroll Views at
https://developer.apple.com/videos/play/wwdc2010/104/
Very simple example, how to show scrollView when user rotates device to landscape:
//header
// tableView and scrollView has the same superview
// scrollView is placed above tableView
#property(nonatomic, retain) IBOutlet UITableView *tableView;
#property(nonatomic, retain) IBOutlet UIScrollView *scrollView;
//implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration
animations:^(void) {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.scrollView.alpha = 1.0f;
} else {
self.scrollView.alpha = 0.0f;
}
}];
}
I would put the UITableView inside the UIScrollView. This gives you the horizontal scrolling.
But I'm a litte bit confused: The 2nd screenshot shows a UINavigationController?