iOS 8 UILabel text is null issue - ios

I have an issue in the iOS 8 with the UILabel. I am printing a text on the view while the text won't remove from the view if the result1 is bigger than the result2 while it is working fine in the iOS 7. And I have debugged and found that the label1.text is always null before I remove it from the screen. Please where would be my issue?
-(void)warningAlert{
label1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 85, 300, 660)];
label1.backgroundColor = [UIColor clearColor];
label1.textColor=[UIColor redColor];
label1.numberOfLines=0;
[self.view addSubview:label1];
if (result1 < result2)
{
if (printed == NO) {
label1.text = #"“Warning Warning!!”";
printed = YES;
}
}
else{
NSLog(#"check the value %#", label1.text);
[label1 removeFromSuperview];
printed = NO;
}

Instead of adding and removing the label.
Alloc the memory to label only once in viewDidlload
And then Just hide or unhide the label.

Related

UITextView input cursor issue in iOSsimulator

I am trying to create dynamic UITextViewController for my iOS application.So from my ApplicationViewController I am trying to get the TextView.
Textview is coming properly but the input cursor for my UITextView in coming down.How to get the blue input cursor from the top?
TextViewController.m
+(UITextField *)prepareUITextView
{
UITextView *uiTextView= [[UITextView alloc] initWithFrame:CGRectMake(
10, 80, 300, 100)];
uiTextView.layer.borderWidth = 1.0;
uiTextView.layer.cornerRadius = 8;
uiTextView.font = [UIFont systemFontOfSize:15];
uiTextView.autocorrectionType = UITextAutocorrectionTypeNo;
uiTextView.keyboardType = UIKeyboardTypeDefault;
uiTextView.returnKeyType = UIReturnKeyDone;
[uiTextView becomeFirstResponder];
uiTextView.selectedRange = NSMakeRange(0, 0);
return uiTextView;
}
ApplicationViewController.m
[self.view addSubview:[TextViewController prepareUITextView]];
Try this code
uiTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Adjust the Top value the way you want. this will solve your issue.

UITextFields aren't allowing me to edit text

I've programmatically created two UITextFields in my iOS app, and set their text to the _minPrice and _minPrice variables, respectively.
The _minPrice and _maxPrice values appear correctly in the two fields, but tapping on them doesn't allow the user to edit them, they just remain those static values, backspacing doesn't work. Is there anything about my code thats preventing the text fields from being edited?
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view addSubview:view];
Your issue is clearly the frames you're setting...
Setting the color of the view you add the labels to to blue reveals your problem:
If you ensure that the labels are actually within the view you add them to (i.e. not negative), editing will be fine. All I did was change the negative x and y values in tf to positive, and they were editable:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];
Try this! Maybe there is another view at the top of the textField
your_textfield.userInteractionEnabled = YES;
If this doesn't work
add another line
[self.view bringSubviewToFront: your_textfield];
Try to add delegate methods on your textfields. Like
- (void) textFieldDidEndEditing:(UITextField *)textField
{
// ADD BREAKPOINT HERE.
}
Check if it goes to that line of code. If not maybe there's a view on top of it. Or you can try to bring textfield to front like .
[self.view bringSubviewToFront:yourTextfield];
But this isn't a good example of how you fix the problem. Just to test if there is a view on top of it.

Add UILabel as a constant field to left of UITextField

I am facing on problem, I want to have a static text in UITextField.
Text like "UserName:" should appear constantly to the left side of UITextField. Editing the UITextField should start from where the "UserName:" text ends.
For example, in iPhone settings, if we go to Twitter app and try to add new account. The way the user name and password textfield looks like. I want to develop same like that.
I don't understand why everyone has has posted such off the track answers to such a simple issue.
You simply do this:
Create a UILabel, with UI complimenting you textField.
You set it's text (Username:, etc).
Assign it to your textField's leftView property.
That's it. You will have to check frames, but that is basic, and I believe you can do that without much effort.
UPDATE: Sample code
UITextField* aField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
aField.placeholder = #"Please enter a username";
aField.layer.borderWidth = 1.0f;
aField.backgroundColor = [UIColor whiteColor];
aField.layer.borderColor = [UIColor lightGrayColor].CGColor;
aField.layer.cornerRadius = 3.0f;
UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 38)];
aLabel.text = #"Username:";
aLabel.font = aField.font;
aLabel.textColor = [UIColor grayColor];
aField.leftView = aLabel;
aField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:aField];
Only needs minor adjustments.
Try to use two text fields (one partially overlap with other) or Implement the below code.
-(void)viewDidAppear:(BOOL)animated{
yourTextField.text = #"UserName:";
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//handle back spaces and error conditions
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([editedText isEqualToString: #"UserName"])
{
return NO;
}
else
{
yourTextField.text = editedText;
return YES;
}
}
Below code may cater your need.
UIView *vis = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 50)];
UILabel *lblUserName = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 40)];
UITextField *txtUserName =[[UITextField alloc]initWithFrame:CGRectMake(125, 5, 180, 40)];
lblUserName.text= #"User Name";
txtUserName.placeholder = #"#User Name";
[vis addSubview:lblUserName];
[vis addSubview:txtUserName];
[vis setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:vis];
Regards,
Amit
Please try this code. I am sure this will help you.
UILabel *lblLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
lblLeft.backgroundColor = [UIColor clearColor];
lblLeft.text = #"Name: ";
lblLeft.textColor = [UIColor lightGrayColor];
lblLeft.font = [UIFont systemFontOfSize:14.0];
txtYourTextField.leftView = lblLeft;
Happy Coding :)
Deepak
For quick solution, do that with two overlapping textfields like this:
Then change the textfield border style to No border style and disable the user interaction of the below textfield:

Text is not properly visible in UITextField

UITextField *theLastNameTF = [[UITextField alloc]init];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
I'm creating a textfield using the above code.I need to show a line under the textfield,so i'm adding a gray coloured UILabel to text field.
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.size.height-0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}
I'm facing a issue in textfield,while entering texts,the text are half visible and cursor is not moving towards left of text.
Please refer the image,I have entered "arun... arun 123",as expected the cursor should be at left of 123,but the cursor is not moving further.
How can i fix this this?
EDIT
This code is to reframe the UI
(call layoutViewForCurrentorientation () inside orientation change method)
-(void)layoutViewForCurrentorientation
{
if ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationPortrait) {
[self potraitUI];
}
else
{
[self landscapeUI];
}
}
If i remove the line textField.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0]; .I issue is resolved but i need bigger font in textfield.
I was using font [UIFont fontWithName:#"Helvetica-neue" size:24.0] and the textfield size was 25.I made my UITextfield height to 30 and the issue got resolved.Don't know the actual reason but it worked.
Please set the textfield frame
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(50,50,250,30)];
I hope it helps you.
I'm seeing the same problem in 9.3. Appears to be an iOS bug.
When the UITextField is first responder, the UITextField contains a scrollview (UIFieldEditor) which contains the input view (a _UIFieldEditorContentView, -[UITextField inputView].) The bug is that the input view is the incorrect size. The input view is not wide enough and thus the scrollview's -contentSize is also incorrect. The UITextField is scrolling as far right as it can, but that's not far enough because the input view is too small.
My workaround is to reset the text field's text. You'll also probably need a flag to ignore delegate callbacks:
_ignoreTextDelegate = TRUE;
NSString *text = textField.text;
textField.text = nil;
textField.text = text;
_ignoreTextDelegate = FALSE;
Try this
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(20,50,250,30)];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:kHelveticaneue size:15.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
And set the line frame's y value should be with respect to textField frame's y value
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.origin.y + 0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}

removeObjectFromSuperView not working with if statement

I have a button that when pressed is supposed to hide the status bar and place text in its spot. Then, when the button is pressed the label is supposed o be removed from the view and the status bar will re-appear. The first part works -- the status bar is hidden and the label is placed on the view, the problem is when I press the button a second time (to remove the text and put back the status bar). The status bar re-appears but the label is not being removed from the view. To achieve this I am using an if statement. I am also using removeObjectFromSuperView which is the thing that is not working.
Here is the code:
- (IBAction)buttonPressed:(id)sender {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 21)];
label.text = #"This is a test";
[label setFont:[UIFont systemFontOfSize:13]];
label.textAlignment = NSTextAlignmentCenter;
if (hidden == NO) {
[UIApplication sharedApplication].statusBarHidden = YES;
[self.view addSubview:label];
hidden = YES;
}else if (hidden == YES) {
[UIApplication sharedApplication].statusBarHidden = NO;
[label removeFromSuperview];
hidden = NO;
}
}
Thanks
Edit: Here is what the problem looks like:
You need to track your label outside of the buttonPressed: method. Right now you are creating a new label every time the button is pressed then removing that same label.
So for example:
#interface MyViewController () {
UILabel *label;
}
- (IBAction)buttonPressed:(id)sender {
if (hidden == NO) {
[UIApplication sharedApplication].statusBarHidden = YES;
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 21)];
label.text = #"This is a test";
[label setFont:[UIFont systemFontOfSize:13]];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
hidden = YES;
} else if (hidden == YES) {
[UIApplication sharedApplication].statusBarHidden = NO;
[label removeFromSuperview];
label = nil;
hidden = NO;
}
}
You can also set label = nil; once you remove it from the super view.

Resources