iOS Swift UITextfield adjust fontsize width - ios

How to adjust UITextfield font size when it is hold long character?
I tried adjustsFontSizeToFitWidth. it is not working.
any help will be appricated.thanks in advance

With a UITextField, text must fit on one line and cannot wrap.
You have two options:
Shrink the font to fit on one line:
self.TextFieldExample.adjustsFontSizeToFitWidth = YES;
self.TextFieldExample.minimumFontSize = 10.0; //Optionally specify min size
Use UITextView to enable text wrapping:

Working Swift 4 Solution
Make sure you set adjustsFontSizeToFitWidth to true and then set your minimumFontSize. This will scale the text inside the UITextField to fit the width of your UITextField with a minimum font size equal to or greater than what you set minimumFontSize to. When the text is still too long to scale down to fit at the minimum font size it will truncate the rest.
textField.adjustsFontSizeToFitWidth = true
textField.minimumFontSize = 10

You have to set the width first
textField.frame = CGRect(0, 0, your width, your height)
And
textField.minimumFontSize = 0.5

Related

Calculate width required to fit text for a multiline UILabel?

I am trying to expand the width of a 2 line UILabel to accommodate a varying amount of text. To do this I am attempting to calculate the width required and then setting it to a width constraint I have set on the label.
However, everything I have done ignores the height/word wrapping, and returns the width required for a single line of text (not 2 lines). Any ideas on how i can find the width needed if 2 or more lines are used in the label?
I have tried using the following methods to find the width:
1.Using boundingRectWithSize to determine the appropriate size for a fixed height (height of 2 lines)
CGSize maxSize = CGSizeMake(MAXFLOAT, fixedHeight);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = #{NSFontAttributeName: self.font, NSParagraphStyleAttributeName: paragraphStyle};
CGRect rect = [self.text boundingRectWithSize: maxSize
options: (NSStringDrawingUsesLineFragmentOrigin)
attributes: attributes
context: nil];
Using the sizeThatFits method to determine the appropriate size.
Thanks!
Make sure fixedHeight you are passing is sufficient enough to accommodate multiple lines. To be safe you can use following
CGSize maxSize = CGSizeMake(300, CGFLOAT_MAX); //where 300 is the width of label
Setting above height will make sure it takes into consideration multiple lines as width is limited. In previous case you were setting width to MAXFLOAT thus width was big enough to accommodate entire content in single line.
It might be silly, but make sure that 'numberOfLines' property of the UILabel is set to 0, so that the size calculation methods can use multiline options.

Make background UILabel bigger

I have a question in my app I have a number label (IBOutlet). And when I write self.numberLabel.backgroundColor = [UIColor redColor]; is showing a red color fit in the height of number, All I want is to make the background a little bit bigger. Thanks
self.numberLabel.frame = CGRectMake(self.numberLabel.frame.origin.x, self.numberLabel.frame.origin.y, widht, newHeight);
and to make sure font stay same size if needed
[self.numberLabel setFont:[UIFont systemFontOfSize:35]];
You have to set constraints in Interface Builder / Storyboard to fix your label height/width.
If your label's content changes in width, you can use this to calculate a new width with a bit of space left:
float labelWidth =
[self.myLabel.text
boundingRectWithSize:self.myLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{ NSFontAttributeName:self.myLabel.font }
context:nil]
.size.width;
CGRect rect = self.myLabel.bounds;
rect.size.width = labelWidth + 15.0f; //replace 15.0F with the value you want
[self.myLabel setBounds:rect];
There're many ways to skin a cat... in my case the content of the "uilabel" determines its size. I just want the background to be slightly bigger -- 5 points vertically, 7 points horizontally. So I use autolayout to solve it.
Add a uilabel as a subview to the background which is a uiview
Add constraints in IB between the uilabel and the uiview
Add constraints in IB between the uiview and its superview

How to adjust UILabel font size to fit the fixed Rectangle (for iOS7 +)?

I know this question has been asked so many times and I have seen most of the solutions like
How to adjust font size of label to fit the rectangle?
How to calculate actual font point size in iOS 7 (not the bounding rectangle)?
NSString sizeWithFont: alternative in iOS7
What I want is that I want to adjust the font size in a given rect with WORD WRAPPING technique. The posts I mentioned above somehow adjust the font size but none of them is using word wrapping technique. Let say, I have a label
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 25)]; \\60px width
tempLabel.text = #"Internationalisation";
tempLabel.numberOfLines = 2;
Now it should wrap the text in one line (within 60 px) with minimum possible font. But it keeps the text in 2 lines with a bit bigger font and the text breaks up like "Internationali" in first line and "sation" in second. How can we apply word wrapping in it.
tempLabel.adjustsFontSizeToFitWidth = YES;
does not seem to work for two or more lines and
CGSize size = [self sizeWithFont:font
minFontSize:minFontSize
actualFontSize:&actualFontSize
forWidth:maxWidth
lineBreakMode:self.lineBreakMode];
is deprecated in iOS7+
So how can I resolve my Issue. Please do help. This is so important for me.

UILabel with constant size and dynamic font size depends on text

I have UILabel on my UIView. I always get various data for my label(from backend). I have some rect to display text on View (i set it as uilabel size, this size is constant).
How to set dynamic font for my UILabel for its size?
I just need to display text data with actual font size, for my rect bounds. If text really long, and minimal font will wrap text - i need to apply minimal font size to display more text data.
(my label can be multi lines to be sized to const rect bounds)
There is property in UILable - adjustToFitWidth in xib, where you can give minimum font size. So if your text is getting longer then it will automatically reduce font size to adjust it within the label.
Note: whatever value given in adjustToFitWidth, is minimum limit, so your font size always be greater or equals to that value.
Enjoy Coding :)
I would do this in this way: Get the NSString character count. Based on that character count, calculate the size.
NSString *myString = #"Hello";
int myInt = myString.length;
float fontSize = PUT_A_VALUE_HERE / myInt;
You are going to play with the variable PUT_A_VALUE_HERE until you get desired results. After that, change the font size
[lblYourUILabel setFont:[UIFont fontWithName:fontName size:fontSize]];

iOS multiline label in Interface builder

How can I make a multiline UILabel in interface builder for iOS? I tried the UITextView but it didn't quite suit my needs.
How can I add multiline (text) in label?
You can use numberOfLines property which defines maximum number of lines a label can have. By default, it's 1. Setting it to 0 means the label will have unlimited lines.
You can do it in code:
textLabel.numberOfLines = 5 // for example
Or in Interface Builder:
Hit Control+Enter to add a line in UILabel in Interface Builder/Storyboard.
Thanks AppleVijay!
Also to call sizeToFit, like this:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit];
The height will be automatically computed.
set width of label as u needed small then use IB to set line breaks to word wrap
or use with code like this
I found a solution.
One just has to add the following code:
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Set number of lines zero for dynamic text information, it will be useful when your text are varying.
Programatically (Swift 3)
var label = UILabel()
let stringValue = "iOS\nmultiline\nlabel\nin\nInterface\nbuilder"
label.text = stringValue
label.numberOfLines = 0 // Set 0, if number of lines not specified.
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame
Using Inetrface Builder
Note: It is not required to set Minimum Font Scale, but nice to have a minimum scale factor to fit text into label frame.
Number of lines is visible in IB with Plain UILabels
set lines field as 0 . It will create multiple lines as per the provided space to the label.
In iOS7 (Xcode5) you shold set the lines of UILabel to 0 for unlimited multiple input in storyboard.
The most important is to set the height of the UILabel can hold the lines of input you are going to set.
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
CGSize size = [[[arrNewsFeed objectAtIndex:row] objectForKey:#"c"] sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(188, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
textLabel.frame = (CGRect){.origin = cell.lblNewsDescription.frame.origin, .size = size};
If you set the numberOfLines property equal to 0, the label will automatically adjust to the required number of lines of the given text.
I had been struggling with this for a long time.
I just wanted my label text to appear on the second line. But whatever I do, it would just overflow the UILabel box. For me, changing the autoresizing in size inspector worked. Simple fix.
May be someone might find it helpful who is looking for something similar.
For X-Code 7.2
-- Select UILabel
-- Attributes inspector
-- Text - Select Attributed
After this you can see some more attribute you can add into you label, in which you can also find number of Lines. Which make your label multiline.

Resources