How to add text area in iOS app - iphone

I am newbie for iPhone application. I want to add TextArea. I know how to add textField, however can someone help me how can I have Textare in my iPhone application?
I thought, I can increase the height of textfield and make it multi-line, however that option is not there.

You cannot increase the height of UITextField. for this you need to use UITextView.
In your Interface builder where you got UITextField, there is also an option of UITextView.
you can use UITextView same as UITextField in iPhone. UITextView is used for multiple lines
UITextView *txt = [[UITextView alloc]init];
//set frame
//add on view
[self.view addSubView:txt];
[txt release];
I hope this solves your problem Sample Code for TextView

UITextView *txt = [[UITextView alloc]initwithframe:CGRectMake(x, y, width,height)];
[self.view addSubView:txt];
[txt release];

You could simply do this by clicking on Mainstoryboard.storyboard or viewController.xib and dragging in a UITextView This will allow multi-line text. If you need to access the Textview, type:
.h file
{
IBOutlet UIITextView *textView;
}
#property (nonatomic, strong) IBOutlet UITextView* textView;
.m file
#synthesize textView;
EDIT
to get the users data, hook up the outlet in storyboards,
then use:
-(void)getUserText{
NSLog#"%#", self.textView);
}
This should print out whatever the user entered into the textView.

Related

UIScrollView stops working when label text in UIScrollView is changed

I have had this problem for a long time now, I hope stackoverflow can help!
THE PROBLEM IS
When I enter "Alan Shearer" into the EnterNameText (UI TEXTFIELD) the label changes text, however the ScrollView no longer scrolls to the set content size, it will scroll a little bit, but no were near the set content size, in this case 2000px in height.
details
1) Xcode 4.5.1
2) IOS 6
In my .xib file ( tinypic.com/view.php?pic=2h4yx3o&s=6 ) I have a UIScrollView (320 pixels in width, and 490 pixels in height)
The UIScrollView contains labels that are all connected to view controller.h.
The Label (all labels are set in the same way) :
#property (strong, nonatomic) IBOutlet UILabel *AlanShearerLabel;
my UIScrollView in ViewController.h looks like this:
#property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
UIScrollView in ViewController.m
- (void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated]; self.scrollview.contentSize=CGSizeMake(320.0, 2000.0); }
An IBAction called - (IBAction)CheckButton:(id)sender
The action when CheckButton is clicked:
//string 0
NSString *string0 = _EnterNameText.text;
//Alan Shearer
NSString *string1 =#"Alan Shearer";
NSString *string2 =#"Rank 1) Alan Shearer 260 Goals";
NSString *stringshearer =#"Shearer";
//Alan Shearer IF
if([string0 isEqualToString:string1]||
[string0 isEqualToString:stringshearer])
{_AlanShearerLabel.text = string2;
_AlanShearerLabel.textColor = [UIColor greenColor];
_EnterNameText.text=#"";
}
To solve this issue you must be sure to define all the things inside the scrollview before you link the scrollview to the .h file. For example if you have 20 labels in a scrollview, be sure that the 20 labels have been connected to the .h file, set the right size, put in the right place. Only then do you connect your scrollview to the .h file and place the scrollview code in viewDidAppear rather then viewdidload.

Custom TextField With Caption

I am developing an IPhone application and trying to create a custom TextField with caption on it like image below.
As you can see from image Label is changed by the value that user enters or read from database. Also there are other textfields other than name.
I couldn't be sure how should i implement this custom textfield?
One way i think placing an image with "Name:" text to background of textfield and placing "Label" value on it, other way is placing an image with only borders and inserting "Name: Label" text on it.
Are these ways suitable or is there any other best approach available?
Edit: Borders on image are textfield's border; also "Name: Label" is above on textfield.
Simply put another label with the desired text behind or beside the label or text field that is going to be filled. You can set the font and textColor and even the alpha according to your taste.
Make sure that if they overlap (which they should not!) the backgroundColor property of the label is set to [UIColor clearColor].
You could subclass UITextField like this:
#interface LabeledTextField : UIView
#property (nonatomic, strong) UILabel *label;
#property (nonatomic, strong) UITextField *textField;
#end
#define kPercentWidth 0.5
#implementation LabeledTextField
-initWithCoder:(NSCoder)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
CGRect f = self.frame;
_label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,
f.size.width*kPercentWidth,f.size.height)];
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,
f.size.width*(1-kPercentWidth),f.size.height)];
[self addSubView:_label];
[self addSubView:_textField];
}
return self;
}
#end
You could then use it like this: insert a UIView into your storyboard view controller and change the class to your LabeledTextField. This would ensure initWithCoder is called. Otherwise, you might have to put the init code into its own setup function and call it from your override of initWithFrame. Make sure you wire up the view with your outlet
// .h
#include LabeledTextField.h
//...
#property (nonatomic, strong) IBOutlet LabeledTextField *labeledTextField;
// .m, in some method
labeledTextField.textField.text = #"editable text";
labeledTextField.label.text = #"non-editable text";
Similarly, you could modify all properties of the label and text field, including colors, fonts etc.

How to assign multiple UIButtons to a UITextView

I want to assign two UIButtons to UITextView so that when one of the buttons is pressed the textview content should change from what it had when the previous button was pressed.
MyViewController.h:
#interface MyViewController : UIViewController
{
NSString* savedText;
}
#property(nonatomic, retain) IBOutlet UITextView* textView;
- (IBAction)buttonOnePressed:(id)sender;
- (IBAction)buttonTwoPressed:(id)sender;
#end
Connect the view controller's textView outlet to the text view in the xib file. Connect the buttons to the corresponding actions. (See Apple's Xcode tutorial if you don't know how to do this.)
MyViewController.m:
- (void)buttonOnePressed:(id)sender
{
[savedText release];
savedText = [textView.text copy];
}
- (void)buttonTwoPressed:(id)sender
{
textView.text = savedText;
}
(These aren't the complete files, of course, just the bits to make it do what you're asking.)

Very simple iPhone App crashes on UILabel settext

I have a very simple application. I have a button and a label in IB. I have an IBAction for onClick that calls setText on the label. There's an outlet for the label. Everything is connected in IB. It crashes the app the first time in the simulator. When I launch it again, it sets the text. Then crashes again next time. It always crashes on the actual device. This should be simple, but I'm not sure what I'm doing wrong.
Thanks.
in my .h file :
#import <UIKit/UIKit.h>
#interface UntitledViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIButton *button;
}
#property (nonatomic, retain) UILabel *label;
-(IBAction) onClick1: (id) sender;
#end
and in the .m:
- (IBAction) onClick1: (id) sender
{
//[label setText:#"Hello World!"];
label.text = #"Hello World!";
//[button setTitle:#"Clicked" forState:UIControlStateNormal];
}
Sorry, I'm new to the site. How do I get the crash log and the stack? Thanks.
EDIT : While this answer is technically correct, it doesn't answer the question at all :( Sorry
< warning - this is a guess >
If you're getting a crash setting the label's text then it tells me that you have set a value to label in the past but it's not been retained correctly.
I'm guessing you have code like this :
label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];
when you should have code like
// Option 1
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];
or
// Option 2
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)];
( the first one uses the property to retain to label. The second one doesn't autorelease it. The first one is the recommended way )
Double check you set connection for label in IB.
Put breakpoint in debugger on line label.text = #"Hello World!";
And make sure label is not nil here.
If it is nil you didn't set connection in IB for it.

How to position your custom UIView control derived from UITextField

I have extended UITextField so that I can use my custom myInputView. But I am having trouble positioning the myInputView to a desire location. Doesn't matter what i do to set the CGRect frame of myInputView it will position the height be at the bottom of the screen.
See Screenshot:
As you can see in the picture the reversed number pad is my custom inputView, but I want to be able to position it towards the middle of the screen.
MyTextField.h
#interface MyTextField : UITextField {
UIView *myInputView;
}
#property (nonatomic, retain) IBOutlet UIView *myInputView;
#end
MyTextField.m
#implementation MyTextField
#synthesize myInputView;
- (UIView *)inputView {
NSLog(#"Return inputView");
CGRect frame = CGRectMake(0, 200, 320, 215);
[myInputView setFrame:frame];
return myInputView;
}
- (void)dealloc {
[super dealloc];
[myInputView release];
}
#end
You might want to consider NOT implementing this in terms of an input view. Setting the input view lets the system treat it like it treats the keyboard. You DON'T get to set the frame, only the height. I would recommend getting rid of the UITextField, and replacing it with a UILabel with a UIButton over it. Add your custom input view to the main view, and position it offscreen. When the button is pressed, animate your custom input view back on screen. When the user makes inputs, just update the label. Don't worry about first responder or the UITextField.inputView at all.