How to Resize UITextField Placeholder Text to Fit UITextField width? - ios

Can Change UITextField Placeholder Text Color as follows:
[txtField setValue:[UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]
forKeyPath:#"_placeholderLabel.textColor"];
Similarly is there any way to Resize UITextField Placeholder Text Font to Fit UITextField width?
Note: I have text fields in custom cell of table view and also using autolayout in custom cell.

I met the same issue, here is the solution:
UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:#"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;

Here is one coding that i used in ViewDidLoad to set the placeholder of textfield of desirable size.
-----------------------------------code-----------------------------------------
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
textfieldUsername.leftView = paddingView;
textfieldUsername.leftViewMode = UITextFieldViewModeAlways;
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
[textfieldUsername setValue:[UIColor lightTextColor] forKeyPath:#"_placeholderLabel.textColor"];
(Nj)

You have the placeholder text and you have the frame of the uitextfield .
From these two you can calculate the font size which fits and assign as a Attributed Placeholder String.
If calculated font size is less than your limit , then use that font size other wise use the limit

Here is code
textField.placeholder = #"Pavankumar";
[textField setValue:[UIColor greenColor]
forKeyPath:#"_placeholderLabel.textColor"];
see below code it change font of the placeholder
textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:#"Pavankumar" attributes:#{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue-light" size:12.0]}];

Related

How can we fix UItextfields text and UIbuttons title labels exactly at center position

Hi i am beginner in ios and i am trying to insert UItextfield "text" data exactly at center of the y-axis as like below textfield image
And i would like to insert UIbutton "titleLabel text" exactly center of x-axis and center of y-axis as like below button image for this i tried lot but i did not get result please help me some one
my code:-
[self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
self.TripIdTxt.backgroundColor = [UIColor whiteColor];
self.TripIdTxt.layer.cornerRadius = 3.0f;
self.TripIdTxt.layer.masksToBounds = YES;
[self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
self.TripIdTxt.textColor = [UIColor blackColor];
self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];
set
for UIButton
yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;
for y-axis
[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
for X-axis
[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
for UITextFields
yourtextfieldName.textAlignment = NSTextAlignmentCenter;
diectly you can access in storyboard like
for additional information see this link
Padding with TextField
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;
or else use Transform
TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
Set the text alignmaent center of your lable property .
mainlabel.textAlignment = NSTextAlignmentCenter;
u set its alignment using xib by
u can also set its vertical alignment using xib by
Programmatically U can set Content's vertical alignment for textfield as well as button as
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;
Hope it helps.
Happy Coding.. :)

label disappear when use size to fit?

I am displaying a label on UI in iOS using code, not from storyboard. When the size of the label is large then it does display accurate it go outside the view so I decided to use sizeToFit in iOS. But when I apply this thing on label then it does appear on view. Please tell why my label does not appear in this case.
Here is the code for the label:
self.label_company = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 150, 100)];
[self.label_company setFont:[UIFont fontWithName:#"HelveticaNeue" size:12]];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 150, 100)];
[label1 setFont:[UIFont fontWithName:#"HelveticaNeue" size:12]];
label1.text=#"yourdata";
[self.view addSubview:label1];
[label1 sizeToFit];
Make sure you have some text in your label as well as add it to your view.
You currently have no text set to the label hence calling sizeToFit is making its width 0 as there is no size of the label in terms of width.
Set self.label_company.text = #"Some text" and then call sizeToFit
Ensure that you set the text of the label before calling size to fit.

UITextField sizeToFit leaves right margin

I have a UITextField that I'm trying to center in a UIView. However, when I call sizeToFit it leaves a small right margin in the textField which causes the text to be slightly off center when the textField background is clear. I've colored it here so you can see what I'm talking about:
messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
messageField.delegate = self;
messageField.font = [UIFont systemFontOfSize:15];
messageField.textColor = [UIColor darkGrayColor];
messageField.textAlignment = NSTextAlignmentLeft;
messageField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"not fitting right" attributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
[messageField layoutSubviews];
[messageField sizeToFit];
messageField.backgroundColor = [UIColor lightGrayColor];
And the textfield looks like this (I would post a picture but need more reputation points, this is basically the same though):
not fitting right
Any help would be greatly appreciated!
I should not that if I create a UILabel of the same font size and text, call sizeToFit on the label and then assign the UITextField frame to be the same as the UILabel, it works like a charm.

UIButton - Lead Text with Icon

I am trying to figure out a solution to creating a UIButton, with centred text which has a UIImage (icon) leading the text, so its sites just in front of the button title.
I, however am struggling to think up a way of doing this as you cannot retrieve the position of the text. Any thoughts? This must be a fairly common thing to do.
Use this code
UIButton *scoreButton=[UIButton buttonWithType:UIButtonTypeCustom];
scoreButton.frame=CGRectMake(0,0,100, 100);
//scoreButton.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 2);
scoreButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
scoreButton.titleLabel.font=[UIFont boldSystemFontOfSize:13];
[scoreButton setTitleColor:[UIColor colorWithRed:0.9411 green:0.5647 blue:.2916 alpha:YES] forState:UIControlStateNormal];
[scoreButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIImageView *scoreButtonImageView=[[UIImageView alloc]init];
scoreButtonImageView.frame=CGRectMake(0,35,30 ,30);
scoreButtonImageView.image=[UIImage imageNamed:#"leaderboard_score_button.png"];
[scoreButton addSubview:scoreButtonImageView];
Use UIEdgeInsetsMake to set your text start and end points.
In this way your image will be on the extreme left hand side and you can write text after the image
Create a UIView subclass that has a UIImageView and UILabel. Position the label to the right of the image view within this view. Add this view to your button and position it horizontally and vertically centred.
I think , it will help you. Always careful with setImage: and setBackgroundImage:
UIButton *yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateNormal];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[yourBtn setTitle:titleString forState:UIControlStateNormal];
Use following method :
UIImageView *yourPlusSign = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"yourPlusSignImageTitle"];
yourPlusSign.frame = CGRectMake(x, y, width, height);//choose values that fit properly inside the frame of your baseButton
//or grab the width and height of yourBaseButton and change accordingly
yourPlusButton.contentMode=UIViewContentModeScaleAspectFill;//or whichever mode works best for you
[yourBaseButton addSubview:yourPlusSign];
Here is Ref : Add an image inside UIButton as an accesory
you can also subclass UIButton it is a better way to do it.
If you use font images like me you can do it with a NSParagraphStyle
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
UIFont *font1 = [UIFont fontWithName:#"Avenir-Roman" size:14.0];
UIFont *font2 = [UIFont fontWithName:#"Icon-Moon" size:12.0];
NSDictionary *fontSyle1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style};
NSDictionary *fontStyle2 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[IcoMoon iconString:k12_ADD_NOTE] attributes:fontStyle2]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#" Add Note" attributes:fontStyle1]];
[_addNewBtn setAttributedTitle:attString forState:UIControlStateNormal];
[[_addNewBtn titleLabel] setNumberOfLines:0];
_addNewBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
_addNewBtn.layer.borderWidth = 1;
[_addNewBtn setBackgroundColor:[UIColor whiteColor]];
[_addNewBtn setTintColor:[UIColor darkGrayColor]];
This where you can get icon fonts https://icomoon.io/
This part is part of a way to convert char's to strings
[IcoMoon iconString:k12_ADD_NOTE]
you can find out more by going to https://github.com/sebastienwindal/IOSIcoMoon

Positioning UILabel inside a UITextView

Trying to position a UILabel above some text, but the elements are "stepping" on each other. I positioned them with x,y,h,w such that they should not do this but alas, they are. You can see in the screenshot that the label text "Abalone" and the text are commingled. What changes do I need to make here? I've already tried changing the x,y,w,h values to no great effect...
txtView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,280,300)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UILabel *wordTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
wordTitle.text = word.term;
[txtView addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
I think the problem is you are adding the label view as a subview to UITextview. You have to reserve some space for the label view in the UITextview. You can either add some padding to the textView, which can be confusing, because padding in UITextView is not constant and varies based on the font size. Or you can add your UILabel directly to the detailsViewController.view and start your UITextView below the label.
Hope it helps.

Resources