Increase font size based on the iphone screen size [duplicate] - ios

This question already has answers here:
How can we keep a text field's font size relative to the screen size?
(2 answers)
Closed 5 years ago.
How to change font sizes automatically of all buttons, label title based on the devices (iphone/ipad) in the UIStoryboard? If i change in programmatically there are my different screen sizes how can i change for all the components. Is there any way to do that in proper way ?

Try This For Swift
Create function.
func setCustomFont() -> CGFloat {
//Current runable device/simulator width find
let bounds = UIScreen.main.bounds
let width = bounds.size.width
// basewidth you have set like your base storybord is IPhoneSE this storybord width 320px.
let baseWidth: CGFloat = 320
// "14" font size is defult font size
let fontSize = 14 * (width / baseWidth)
return fontSize
}
Use this function.
yourbutton.titleLabel?.font = UIFont.init(name: "Helvetica", size: setCustomFont())
This type you also set UILabel and UITextfield font but this not use for UIButton.
1.Go to storyboard select UILabel/UITextfiled.
2.Go in Attribute Inspector
3.Check Dynamic Type option Automatically Adjust Font.

If you are using the interface builder you can set separate size classes for the elements and adjust accordingly

You can use AutoShrink like this:
yourButton.titleLabel.minimumScaleFactor = 0.5;
yourButton.titleLabel.adjustsFontSizeToFitWidth = YES;
This won't adjust the font size bigger than we set.But it will increase based on phone size upto maximum size that we set.

Related

dynamic textview with arabic text entry

I am using a dynamic textview.
func textViewDidChange(_ textView: UITextView) {
let str = textView.text! as NSString
let size = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20.0)])
textView.frame = CGRect(origin: textView.frame.origin, size: size)
}
This is the code used for dynamic change in height and width. This works well while using "English". The problem is when i change language to "arabic", text view has to increase width towards the left, but it still increasing the width towards the right.
First check the iPhone screen co-ordinate system below.
I am saying if you increase the width it will always increasing the width towards the right in iPhone. So set your initial origin of the UITextView at the right most and then decrease origin's X Co-ordinate by the amount 'size' you calculate and then set the new origin and size values to the UITextView frame. Hope u understand.
For example :
lets say your initial UITextView origin is at (300,30) and size is (30,30)
now you enter some text Arabic text, and you calculate the size of the text as 100.
Now your new origin's X will be 300 - 100 = 200
so newOrigin = (200,30)
and newSize = (100,30)
now set the UITextView frame as- textView.frame = CGRect(origin: newOrigin, size: newSize)

iOS Swift UITextfield adjust fontsize width

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

Resize a UILabel so it looks the same on iPhone and iPad

I am trying to adapt an existing iPhone only app to support the iPhone 6's as well as iPads. I do not want the app layout to change I just want it to resize to fit the various devices.
A number of UILabels on screen have updatable content which is truncated with an ellipsis if too long.
I would love to be able to do this with autolayout in a storyboard but if code is the only way to do it then so be it.
Any suggestions would be greatly appreciated.
You can programmatically set a font size as a fraction of screen size. Like so:
/*
* Screen size is orientation dependent on iOS 8, so take the shortest
* dimension as a rough measure of screen size
*/
CGSize size = [UIScreen mainScreen].bounds.size;
CGFloat sizeFactor = (size.width < size.height) ? size.width : size.height;
label.font = [UIFont systemFontOfSize:sizeFactor / 16.f];

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.

Get UILabel font pointsize after minimumFontSize

I have a UILabel with a font of point size 17. If I call label.font.pointSize I get 17, which is all good. BBUUUUTTT I also have a minimumfontsize set to 8, now if I cram some text in the label which causes the point size to shrink and then call label.font.pointsize I still get 17 even though I know the point size is smaller
Any ideas how to get the true point size after system has resized the font?
I don't know of an API to get the current point size of the UILabel when it is scaling your content down. You can try to approximate "scaling factor" using sizeWithFont APIs.
Just an idea:
// Get the size of the text with no scaling (one line)
CGSize sizeOneLine = [label.text sizeWithFont:label.font];
// Get the size of the text enforcing the scaling based on label width
CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size];
// Approximate scaling factor
CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width;
// Approximate new point size
CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize;
As savner pointed out in the comments, this is a duplication question. The cleanest solution is found here: How to get UILabel (UITextView) auto adjusted font size?. However, Sanjit's solution also works! Thanks Everybody!
CGFloat actualFontSize;
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Swift 4 and iOS 7+ version (sizeWithFont is now deprecated) of #Sanjit Saluja's answer:
// Get the size of the text with no scaling (one line)
let sizeOneLine: CGSize = label.text!.size(withAttributes: [NSAttributedStringKey.font: label.font])
// Get the size of the text enforcing the scaling based on label width
let sizeOneLineConstrained: CGSize = label.text!.boundingRect(with: label.frame.size, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil).size
// Approximate scaling factor
let approxScaleFactor: CGFloat = sizeOneLineConstrained.width / sizeOneLine.width
// Approximate new point size
let approxScaledPointSize: CGFloat = approxScaleFactor * label.font.pointSize

Resources