I want to make a TableView Cell with a Label and Textfield in it, but the Textfield doesn't appear.
Here is the init of the textfield, it's in a cell-class:
self.tfTitle = [[UITextField alloc]initWithFrame:CGRectMake(20, 70, 100, 20)];
self.tfTitle.borderStyle = UITextBorderStyleRoundedRect;
self.tfTitle.placeholder = #"Titel/Ort";
self.tfTitle.textAlignment = NSTextAlignmentRight;
self.tfTitle.adjustsFontSizeToFitWidth = YES;
[self addSubview:self.tfTitle];
try [self.contentView addSubview:self.tfTitle];
Also change your frame CGRectMake(20, 70, 100, 20) to CGRectMake(20, 10, 100, 20)
70 is too far from the top so that you cannot see it.
Since iOS7, UITableViewCell hierarchy has changed, consider adding the UITextField to self.contentView
[self.contentView addSubview:self.tfTitle];
It might not be the problem, but still, if you make it appear on self, you won't be able to select the textfield.
ALso make sure the frame you give it is in the cell (put some background color to the contentView of the self and to your textfield)
Related
Is it possible to set a UITextField's input range (not the number of characters)? You can check out my screenshot below.
I already tried implementing this: Ellipsis at the end of UITextView
It didn't work, or I'm just doing it wrong.
What I want is to limit the size of the user's input AND make it ellipsis. The text at the left side of UITextfields are merely UILabels set as subviews.
So again... I need help in setting the range of the text input, and if the input length or width overlaps my RED MARK in my screenshot, the rest of the input will be converted to POINTS OF ELLIPSIS. And then I'm pretty sure I can now proceed to my project (example: clicking the arrow button will make a small popup that will show the full text).
I forgot to add my codes in that UITextField:
self.billingAddressTextField = [[UITextField alloc] init];
_billingAddressTextField.frame = CGRectMake(0, 150, scrollView.frame.size.width, 50);
_billingAddressTextField.delegate = self;
_billingAddressTextField.borderStyle = UITextBorderStyleNone;
_billingAddressTextField.background = [UIImage imageNamed:#"textfieldLogIn.png"];
[_billingAddressTextField setTextColor:[UIColor colorWithRed:0.686 green:0.686 blue:0.686 alpha:1.0]];
_billingAddressTextField.backgroundColor = [UIColor clearColor];
_billingAddressTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[_billingAddressTextField setFont:[_billingAddressTextField.font fontWithSize:16.0f]];
[_billingAddressTextField setTextAlignment:NSTextAlignmentRight];
[scrollView addSubview:_billingAddressTextField];
Ok solved, I just added a new Padding (UIView) at the left side of the UITextField (billingAddressTextField). So there are now two paddings (both side) in my UITextField.
UIView *paddingViewForArrowButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, 25)];
_billingAddressTextField.rightView = paddingViewForArrowButton;
_billingAddressTextField.rightViewMode = UITextFieldViewModeAlways;
UIView *paddingforBillingLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
_billingAddressTextField.leftView = paddingforBillingLeft;
_billingAddressTextField.leftViewMode = UITextFieldViewModeAlways;
The scrollable label works fine, but for some reason I cant get the text in the UILabel to center in the UIScrollView, it always stays left aligned. Here is an image of what I am talking about, and my code. (P.S. the UILabel is a subview of the UIScrollView, and the UIScrollView is a subview of the UITableViewCell.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(cell.frame.size.width/21.5, 25, cell.frame.size.width - 40 , 20)];
scrollView.showsHorizontalScrollIndicator = NO;
[cell addSubview:scrollView];
scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 20)];
scrollLabel.text = secondaryLabel.text;
scrollLabel.font = secondaryLabel.font;
scrollLabel.backgroundColor = [UIColor clearColor];
scrollLabel.textColor = [UIColor whiteColor];
scrollLabel.numberOfLines = 1;
scrollLabel.textAlignment = NSTextAlignmentCenter;
[scrollLabel sizeToFit];
[scrollView addSubview:scrollLabel];
scrollView.contentSize = CGSizeMake(scrollLabel.frame.size.width, 20);
I think the code you written for UILabel is pretty much right and the center TextAlignment is there but the problem is you need to pass the right X & Y for UILabel Frame's. For now they are getting set to 0-0 and are adding on the Scrollview's starting frame. This might can solve your problem.
I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.
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.
I would like to position the text in a textfield more specifically.
My current code:
UITextField * textFieldIndustry = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 465, 68)];
textFieldIndustry.font = [UIFont systemFontOfSize:17.0];
textFieldIndustry.placeholder = #"Tap to..";
textFieldIndustry.textAlignment = NSTextAlignmentCenter;
This code:
textFieldIndustry.textAlignment = NSTextAlignmentCenter;
Centers the text but not exactly where I want it. How do I position using for example X & Y?
You cannot set position of text implicitly in UITextField without subclassing it.
You can use UITextView instead, and should use the same approach with it:
UITextView *textView;
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[textView setContentInset:insets];
You can use this approach also with subclassing the UITextField.
Its very simple you just need to do textfield padding on which direction you want by using "UIEdgeInsets"
Ex: UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0);
[textView setContentInset:insets];
It will show you the textfield after 10 pxls from .....like this you can give.
happy coding...
UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = #"Ghi chĂș";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
For the x, you can create a padding by adding a view to the left side of the textfield.
let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: phoneNumberTextfield.frame.height))
textFieldIndustry.leftView = paddingView
textFieldIndustry.leftViewMode = .always
This ensures that the cursor is not at the edg
You can also utilize the leftView portion of UITextView to do what you are asking. This is great for adding a prefix to a textField without placing a label in front of it. I'm not sure exactly how you wish to use it but it may be a good solution. Basically you just create a UILabel (or any view) with the width and height you like then add that as the leftView of the textField.