why setText for textView add text bottom of textView? - ios

I'm noob in objective c and I create one page xib file that this page have one textView empty.
I drag UITextView ton this page and connect to self outlet.
Now I want to add any text like this "I AM JANATAN" in it. when I using this code and run my simulator , my simulator show me my textView but I see my sentence is last line of textView !!!
why??? this is my image : pic of simulator
this is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
getTextPlaces = [self CreateTextViews];
textField1 = [getTextPlaces objectAtIndex:0];
[self.view addSubview:textField1];
NSString *str = [_stack pop];
textArray = [self readInformationFromDatabaseWithData:str];
NSString *s1 = [[textArray objectAtIndex:0]objectForKey:#"Names"];
textField1.text = [NSString stringWithFormat:#"%#",s1]; //this s1 value is I am janatan
}
- (NSMutableArray *)CreateTextViews{
UITextView *tf = [[UITextView alloc] initWithFrame:CGRectMake(35, 70, 250, 50)];
tf.backgroundColor = [UIColor purpleColor];
tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.textAlignment = NSTextAlignmentRight;
//tf.text=#"Hello World";
//second one
UITextView *tf1 = [[UITextView alloc] initWithFrame:CGRectMake(35, tf.frame.origin.y + 70, 250, 50)];
tf1.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
tf1.backgroundColor=[UIColor whiteColor];
tf1.autocorrectionType = UITextAutocorrectionTypeNo;
tf1.textAlignment = NSTextAlignmentRight;
//and so on adjust your view size according to your needs
NSMutableArray *twoText = [[NSMutableArray alloc]init];
[twoText addObject:tf];
[twoText addObject:tf1];
return twoText;
}

my friend for remove this you need is to set the following:
self.automaticallyAdjustsScrollViewInsets = NO;
in the viewDidLoad method.

Related

AddSubView is not working in UIScrollView

I need to place my labels and other objects into a scrollable view. But it doesn't add my 2 labels, I only get a white background everytime I run my program
- (void) initLayout:(CGRect)frame
{
self.backgroundColor = [UIColor whiteColor];
UIFont *customFont = [UIFont fontWithName:#"HelveticaNeue" size:40.0]; //custom font
scrollView = [[UIScrollView alloc] initWithFrame:frame];
[self addSubview:scrollView];
oponent = [[UILabel alloc] init];
oponent.font = customFont;
oponent.textColor = [UIColor blackColor];
oponent.textAlignment = NSTextAlignmentCenter;
oponent.text = #"TEST";
[scrollView addSubview:oponent];
proponent = [[UILabel alloc] init];
proponent.font = customFont;
proponent.textColor = [UIColor blackColor];
proponent.textAlignment = NSTextAlignmentCenter;
proponent.text = #"TRY";
[scrollView addSubview:proponent];
}
You are not setting frames for proponent and opponent labels. Set their frames and also set content size for scrollview. You can also check by setting some border colors to your labels.
To check if your scrollview is working add UIScrollViewDelegate in your header file and then use the delegate methods.
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
Add the following delegate method and use breakpoint to check if it is called
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
You Haven't set frame to label
oponent = [[UILabel alloc] init];
proponent = [[UILabel alloc] init];
set frames
CGRect labelframe // Customize according to your need
oponent = [[UILabel alloc] initWithFrame:labelframe]; // missing
proponent = [[UILabel alloc] initWithFrame:labelframe]; // missing

How to create dynamic uitextfiled inside a loop and make all them editable mode?

I am continuously facing problem to create uitextfield programmatically inside a loop and make all them in editable mode?
I have created successfully text fields inside the loop but problem is that. the last one i.e. the last text field is in editable mode.
EX:
suppose an array (myarray) has 5 values.
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, 0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
i want to make all 5 textfields in editable mode. but as of now, the last textfield is only in editing mode and the rest all are non editable mode.
Can anyone plz suggest me some ideas ?How to achieve this
Thanks in advance.
Ajeet Kumar
First of all you need to provide different frames for that you can them in different position. Your above code looks correct and it must be in editable mode. I execute below snippet on my project and it works like a charm. I am assuming you are using Scrollview and added all textfield in it. Only thing make sure you are not disabling them from
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
or
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string; // return NO to not change
text
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*20, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
The frame of all 5 textField are putting in the same position. You might need to change their frame as well.
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc] init];
roomratetxt.frame = CGRectMake(200, i*60, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}

Changing navigationController title after viewdidload

When I set self.navigationItem.title in viewdidload the title appears correctly, Later based on user interaction I am updating the title. The length of title is fixated to length of the title that is set in viewdidload.
This is want happening in my code
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setTitleTextAttributes:#{
[UIColor brownColor]: NSForegroundColorAttributeName,
[UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0]: NSFontAttributeName}];
self.navigationItem.title = #"My Account";
}
- (void)updateTitleWithUserName {
self.navigationItem.title = #"Test User 167";
}
Result:
Test...
Any suggestion how to fix this?
Try using a label for title so you can adjust its size to fit like this on viewDidLoad, then just change tlabel on update and assign it to the nav title. (Manage color and fonts as you wish)
self.title = #"Your TiTle Text";
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 30.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
try this
- (void)viewDidLoad
{
titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];// create instance variable UILabel * titleLabel;
titleLabel.text=#"My Account";
titleLabel.textColor=[UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size: 16.0];
titleLabel.backgroundColor =[UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView= titleLabel;
}
- (void)updateTitleWithUserName
{
titleLabel.text = #"Test User 167";
}

Can't change text of a UITextField

I have created 2 UITextFields and a UIButton programmatically, when I click the UIButton I want it to change the text of the highlighted UITextField but it's changing the text of the second UITextField not the highlighted one.
-(void)viewDidLoad
{
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
}
- (IBAction)change:(id)sender
{
txtf.text = #"the text was changed"
}
is there any way to solve this problem?
Change your code to
-(void)viewDidLoad{
txtf1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf1.borderStyle = UITextBorderStyleNone;
txtf1.backgroundColor = [UIColor clearColor];
txtf1.font = [UIFont systemFontOfSize:17];
txtf1.autocorrectionType = UITextAutocorrectionTypeNo;
txtf1.keyboardType = UIKeyboardTypeDefault;
txtf1.returnKeyType = UIReturnKeyDone;
txtf1.textAlignment = NSTextAlignmentLeft;
txtf1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf1];
txtf2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf2.borderStyle = UITextBorderStyleNone;
txtf2.backgroundColor = [UIColor clearColor];
txtf2.font = [UIFont systemFontOfSize:17];
txtf2.autocorrectionType = UITextAutocorrectionTypeNo;
txtf2.keyboardType = UIKeyboardTypeDefault;
txtf2.returnKeyType = UIReturnKeyDone;
txtf2.textAlignment = NSTextAlignmentLeft;
txtf2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf2];
}
- (IBAction)change:(id)sender {
txtf2.text = #"the text was changed"
}
And add txtf2 to class members.
UPDATE:
To change highlighted UITextField modify - (IBAction)change:(id)sender to
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}
UPDATE 2
When you have a lot of text fields you can try following
for (id subview in self.view.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = subview;
if (textField.isHighlighted) {
textField.text = #"the text was changed";
}
}
}
txtf always points to second textField. so thats the problem.
When you created the first UItextFied, txtf points to it. But when you create a new UITextField and make txtf point to it, then reference with first textField is lost.
And in your change method, you are always getting the reference of second textfield and changing it.
Two options,
1) use txtf1 and txtf2, two class properties
2) use tags. [UIView viewWithTag:tagValue];
Using Tag's->
Assuming numberOfTextFields you using is 3.
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=1;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=2;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=3;
[self.view addSubview:txtf];
Modify change method to
- (IBAction)change:(id)sender
{
NSInteger numberOfTextFields=3;
UITextField *textField;
for (int i=1; i<=numberOfTextFields; i++) {
textField=(UITextField*)[self.view viewWithTag:i];
if(textField.isHighlighted==YES)
break;
else
textField=nil;
}
if(textField==nil){
NSLog(#"none is highlightes");
}
else{
textField.text= #"new text for highlighted textField";
}
}
Because your both UITextField has same name (txtf), So first change the name of either first or second UITextField. Other wise you code is correct. :)
EDITED :
Just make more correct #Avt's updated answer.
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}

textFieldShouldReturn does not get called (textField loaded programmatically)

I'm currently loading a -UITextField programmatically and trying to make the keyboard go down once I click on 'Search'.
I declare the -UITextFeild inside the .m file as
UITextField *searchTextField;
Inside the .h file I do indeed declare the -UITextField's delegate
#interface firstChannel : UIViewController<LBYouTubePlayerControllerDelegate, UITableViewDelegate,UITableViewDataSource, NSXMLParserDelegate, UITextFieldDelegate, AVPlayerItemOutputPullDelegate>{
This is how I add the UITextField programmatically.
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
I added the delegate inside -viewDidLoad
searchTextField.delegate = self;
However when I click on "Search" on the keyboard nothing happens. I'm unsure why that's happening I've even debugged it by logging something once you hit returns but nothing happens.
Call the searchTExtField.delegate after you create and setup the text field.
Try moving all the code in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad]; searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.delegate = self;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
}
Also, if you are using search bar then you need to use search bar delegate. If you are just using UITextfield, then you should be okay.
Have you implemented the delegate method (this method gets called when the user taps the return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
//Perform search with text:
[self performSearch:[textField text]];
return NO;
}
This will resign the responder. Here you can also call your search method.

Resources