SetLeftView In Label Objective-C - ios

it has been asked before (using textfield, and I'm asking how to include a character not a small icon), and yes I have already tried using this SetLeftView to put a dollar sign '$' or whatever character I want beside the TEXTFIELD.
However, I do not want a textfield, but a label, and when I apply the same code to do what I want, it returns me an error, I mean Xcode fails to build the code.
Here is my code:
// We add a label to the top that will display the results
self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 80, TEXTAREA_WIDTH, TEXTAREA_HEIGHT)];
[resultLabel setBackgroundColor:[UIColor clearColor]];
[resultLabel setText:#"01234"];
[resultLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
resultLabel.textColor = RGB(255,255,255);
resultLabel.textAlignment = NSTextAlignmentCenter;
// Add it to the view
[self.view addSubview:resultLabel];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[resultLabel setLeftView:dollarSignLabel];
[resultLabel setLeftViewMode:UITextFieldViewModeAlways];
Error: No visible #interface for 'UILabel' declares the selector
'setLeftView'. Same error in the line of setLeftViewMode.
Again, this works if I use a textfield.
My working code (using textfield)
// adding a textField for input
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(viewHalf-30, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:#"0"];
[myTextField setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
myTextField.textColor = RGB(255,255,255);
[[self view] addSubview:myTextField];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[myTextField setLeftView:dollarSignLabel];
[myTextField setLeftViewMode:UITextFieldViewModeAlways];

The reason you can't apply that because UILabel doesn't have a method named setLeftView as UITextField do.
What you can do is :
Create two labels next to each other.
Set left labels trailingSpace to right label to 0. Arrange other constraints whatever you want.
Set left label's textAlingment property to NSTextAlignmentRight and right label's to NSTextAlignmentLeft.
Set dolar sign on a left label and numbers to another.

Since a label isn't editable by the user anyway, there is no reason not just to add your $ sign to the label itself.
label.text = [#"$" stringByAppedingString:yourText];
if the special symbol should be an image instead, then look at NSTextAttachment & draw attributed Text

hope this will help u out.
add a dollar image on your label.
override following method of UILabel
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
bounds.origin.x =+leftMargin;
return bounds;
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect: CGRectInset(self.bounds, leftMargin , 0)];
}

Related

Input Range Width Of UITextField iOS

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;

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:

UIStepper valueChanged.

I'm making an iphone app and I have met a problem..
I'm making sub views which contains labels and a UIStepper..
They are made by a for loop like so:
//subView to contain one ticket
UIView *ticketTypeView = [[UIView alloc]initWithFrame:CGRectMake(10, y, 1000, 60)];
if(ticketCount%2){
ticketTypeView.backgroundColor = [UIColor lightGrayColor];
}
[self.view addSubview:ticketTypeView];
//label for ticket type name
UILabel *ticketType = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, 500, 50)];
[ticketType setText:string];
[ticketType setFont:[UIFont fontWithName:#"Helvetica neue" size:20.0]];
[ticketTypeView addSubview:ticketType];
//UIStepper for ticket amount
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(500, 16, 0, 0)];
stepper.transform = CGAffineTransformMakeScale(1.2, 1.2);
[ticketTypeView addSubview:stepper];
//label for price pr. ticket
UILabel *pricePrTicket = [[UILabel alloc]initWithFrame:CGRectMake(620, 5, 100, 50)];
[pricePrTicket setText:#"1000.00 Kr."];
[ticketTypeView addSubview:pricePrTicket];
//totalPrice label
UILabel *totalTypePrice = [[UILabel alloc]initWithFrame:CGRectMake(900, 5, 100, 50)];
[totalTypePrice setText:#"0.00 Kr."];
[ticketTypeView addSubview:totalTypePrice];
Now.. How do I add a IBAction valueChanged for my UIStepper? the stepper is supposed to take the count, multiply it by the pricePrTicket and display it in the totalPrice label..
Any help or hint will be much appreciated :)
You'll need to assign unique tag to all your subviews of ticketTypeView (each should be unique) then follow #thedjnivek answer. When you get call - (void) stepperChanged:(UIStepper*)theStepper method, get totalPrice label object like this,
UILabel *ticketprice = (UILabel *)[theStepper.superview viewWithTag:kTagPriceTicket];
check if label object is not nil,
if(ticketprice) {
ticketprice.text = theStepper.value * pricePrTicket;
}
In your for loop where you're creating ticketTypeView and other labels.
Your label tag should be unique for labels and the same for individual ticketTypeView views.
Create tags like this (you can give any integer for tags),
#define kTagTicketType 110
#define kTagPriceTicket 111
#define kTagTotalTypePrice 112
...
...
...
[ticketType setTag:kTagTicketType]; //NOTE this
[pricePrTicket setTag:kTagPriceTicket]; //NOTE this
[totalTypePrice setTag:kTagTotalTypePrice]; //NOTE this
Write above lines before adding each of the label.
You have to set the target with addTarget:action: like this :
[stepper addTarget:self action:#selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
- (void) stepperChanged:(UIStepper*)theStepper{
//This method would be called on UIControlEventsValueChanged
}
I hope that can help you ;)

UITextField rightViewMode not working correctly

I need to make a custom clear button on a UITextField, so I am using the rightView. Code is below:
UIImage *clearImage = [[UIImage imageNamed:#"search-clear.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0,0.0, 0.0, 0.0)];
UIImageView *clearImageView = [[UIImageView alloc]initWithImage:clearImage];
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = clearImageView.frame;
[clearButton setBackgroundImage:clearImage forState:UIControlStateNormal];
clearButton.backgroundColor = [UIColor clearColor];
self.emailTextField.rightViewMode = UITextFieldViewModeWhileEditing;
self.emailTextField.rightView = clearButton;
self.emailTextField.clearButtonMode = UITextFieldViewModeNever;
However, the button is not showing up at the correct times. It shows when the text field is in focus only with text length 0. As soon as I start typing it disappears. I need to figure out how to replace and duplicate the clear button, so that it shows when the string is at least 1 character.
you can't display both at the same time , but you can do it like this
UITextField * textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 40)];
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
UIImageView * imgvw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"search.jpeg"]];
[imgvw setFrame:CGRectMake(0, 0, 30, 30)];
[textfield setRightView:imgvw];
[textfield setRightViewMode:UITextFieldViewModeUnlessEditing];
[textfield setClearButtonMode:UITextFieldViewModeWhileEditing];
[self.view addSubview:textfield];

Resources