resize my app on another iOS devices? - ios

Iv'e made an app for for iPhone 5, now i want to make it available for another iOS devices, but i don't know how to resize the label in different sizes.
I've got a label that change his point by the iPhone 5 size:
if (a == true) {
self.minutes.frame = CGRectMake(271, 109, 140,100)
}
I used to play with the auto layouts but the label always "jumps" to here:
CGRectMake(271, 109, 140,100)
and back to his first place.
How can i fix it.
Thank you

You can test for the different iphone sizes
if( UIScreen.mainScreen().bounds.size.width == 568)
{self.minutes.frame = CGRectMake(271, 109, 140,100)}
else if(UIScreen.mainScreen().bounds.size.width == 667 )
{ change to the size that looks best in iphone 6 }
else if(UIScreen.mainScreen().bounds.size.width == 736 )
{ change to the size that looks best on iphone 6 plus }
else
{ change to the size that you want for the smaller devices }

You should definitely be using AutoLayout for this, if you tried you need to make sure you edit the constraints to change the position or size of views instead of setting the frame.
Here is a good tutorial that introduces AutoLayout: http://www.appcoda.com/introduction-auto-layout/
Also, UILabel has an intrinsic content size (the size depends on the text and font), therefore you shouldn't be changing it's size manually.

Related

Make responsive label font size for iphone devices

I am new to iOS Development.I am having problem with font size with phone screen size.For example Font size in iPhone 8 Plus looks fine but that text size is bigger in iPhone SE.I tried check Dynamic Type to Automatically Adjusts Font.And try to play with Autoshrink in StoryBoard.And i also tried to Add Font Variation in storyBoard.But I didnt get any good solution.Hope you understand my problem.Thanks in advance
Try this
class func dynamicFontSizeForIphone(fontSize : CGFloat) -> CGFloat
{
var current_Size : CGFloat = 0.0
current_Size = (UIScreen.main.bounds.width/320) //320*568 is my base
let FinalSize : CGFloat = fontSize * current_Size
return FinalSize
}
hope this work
You can change font size by using constraints.
1.take a label give its basic two constraint to satisfy. give one more constraint of equal.width to parent view. keep width as wide as your label text is.(a bit more than label text).
In attribute inspector there is a property name 'auto shrink' set it to 'minimum font size'
thats it.
Note: This will work fine if your Label text is constant. For changeable text there will be other approaches.

How to make text size relative to view in IOS?

In the XCode storyboard, I have set up my ViewController as a bunch of stackviews and everything is relative -- view dimensions are expressed as fractions of other view dimensions... lots of constraints, etc.
This is to make sure it looks decent on all IOS devices (phones and Ipads, anyway).
It does look acceptable in different aspect ratios, but I've noticed that the font size of my UILabels and TextViews are NOT changing -- not getting LARGER along with their containing views.
So, for example, if I switch from an iPhone to an iPad preview, a UILabel size may increase drastically and yet the text that it contains stays the same... so it's tiny text in a big box.
SO... the question is:
Is there a way to express font/text sizes as relative to the view that contains the text?
Something like this:
text.height = 0.7 * container.height
text.width = maintain aspect ratio with height
Thanks.
The same problem i had when i was designing an application for both iPhone and iPad and i tried this solution which is working fine but took a little efforts to manage. You need to create a custom label class which will inherits from UILabel class. There in your awakeFromNib function you can check the device and you can multiply whatever the font size with a ratio you feel ok for iPhone and iPad. You can also add checking for different iPhone sizes. If you wish to use different ratios for different label, make a IBDesignable property named dynamicRatio in your custom class and take that value to increase font. You can play around this. The effort is assigning the class to all your labels and setting properties which i use to do parallel during designing.
Below are the set of code which am using.
import UIKit
class MyLabel: UILabel {
#IBInspectable var autoFont: Bool = false
#IBInspectable var fontSize: CGFloat = 0
override open func awakeFromNib() {
super.awakeFromNib()
let baseHeight: CGFloat = (((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad)) ?1024.0:568.0)
if autoFont == true {
if (isDevice() == DEVICES.iPhoneX) {
let size: CGFloat = 667.0 * (fontSize / baseHeight)
self.font = UIFont(name: self.font!.fontName, size: size)
} else {
let size: CGFloat = UIScreen.main.bounds.size.height * (fontSize / baseHeight)
self.font = UIFont(name: self.font!.fontName, size: size)
}
}
}
}
Hope this idea helps.
Increase text font size of UILabels or UITextFields as you can, then set MinimumFontScale or MinimumFontSize attribute for them in "Attributes Inspector" tab, now font size increases as the UITextFields or UILabels size increases.
UILabel
UITextField

UIbutton misplaced in simulator

Here I have to implement the UIView for both the screens iPhone 5 and 6. So I used the screeHeight to integrate the UIView. But the button in the view is misplaced in iPhone 5s simulator and works fine in iPhone 6 simulator Here is the code for that. Whatever I changed the values in x and y remains the same top position.
if screenHeight == 568{
self.viewFareEstimate.frame = CGRect(x:0, y:150, width:359, height:40)
}
The screen size of Iphone 5 is 320 × 568, but your width is more than 320.
Try this
if screenHeight == 568{
self.viewFareEstimate.frame = CGRect(x:0, y:150, width:320, height:40)
}
Hi #imac for good programming practices try to avoid to give constant values to frame.
For your refrence height for iPhone 5 is 568.0f & 6 is 667.0f are different,
&
Whenever you want size width full then try to give frame like this,
self.viewFareEstimate.frame = CGRect(x:0, y:150, width:self.view.frame.size.width, height:40);// Manage width by + or - constant value now.
In your case check that if you are giving constraint from storyboard already then it should work with setting frame only.

Scale text label by screen size

Is there a way to scale text so that it takes up close to the same screen real estate no matter what the device size is? I've found that the text on an iPad sized device is too small in relation to the screen size when compared to the iPhone. Below is an example of what I'm looking for. Notice the text percentage size is similar in relation to the device screen size.
Example
To set constraints on the label that you have, see this link: How do you make a background image scale to screen size in swift? . I know that you might not be using Swift (I'm using Objective-C), but the first answer shows how to do it in the storyboard. Do the same thing it says, but for the label. Then, see the image below to change the auto shrink options for the label from "Fixed Font Size" to "Minimum Font Scale" (see image below). Hope this helps!
1. Select your label and open attribute inspector for it
2. Click on + sign by Font, select width and height as "Regular", click Add Variation
3. Another Font field will appear, this will represent font for ipad/big screen/ illusion of big screen (scroll view)
4. Select your desired font for ipad
I had my own fix and it's not one click, but I found it well worth it.
Step 1:
Test some font sizes for different screen sizes, recording the font size and the most relevant other dimension.
Here's what I mean...
I'd start with a normal iPad Screen and select a font size that worked.
Since I knew the entire height of the screen would determine what makes a font size comfortable or not, I'd record the font size along with the entire screen height.
So the font size was 50 with a height of 1024.
Then, I switched to the iPhone SE
This was pretty atrocious, so I fixed the font size:
After recording that the font size worked at 25 with a height of 568.
Step 2:
Bust out the calculator app and find a constant that relates the font sizes to the heights (in my case), giving or taking a bit.
Here's what I mean:
I knew 50 works with 1024 and that 25 works with 568.
Dividing 50/1024 gives you .048828125.
Multiplying that by 568 equals 27.734375, which would be a little fat of a font size for the SE.
Repeating that process but using the SE's values to do the division produced .0440140845, and checking that with the iPad's height produces a font size of 45, just a bit small for the mighty iPad screen.
So I split the quotients down the middle, resulting in a number around 0.46.
Step 3:
Connect the labels and change their font sizes programmatically using this number.
First, drag the labels into an outlet collection. I named my connection 'headerLabels' since they're all at the top of the screen.
Then, you can kinda steal my code, but just don't make too much money with it ;)
// Outlet collection of labels
#IBOutlet var headerLabels: [UILabel]!
// Who needs a short variable name when we can have a
// painfully lengthy one to describe the number we calculated?
let relativeFontConstant:CGFloat = 0.046
override func viewDidLoad() {
super.viewDidLoad()
// Should be in the viewDidLoad so it can load the same time as
// the view does.
// apply a font size to all the labels.
for label in headerLabels {
// multiply the height we based it on of the entire view
// to the number we calculated
label.font = label.font.withSize(self.view.frame.height * relativeFontConstant)
}
}
If you've got any questions, comments, compliments, or insults let me know :)
I was having the same issue where I needed the text to be scaled proportionally along with the screen size increase.
Adaptive sizing is quite limited as you can only set the font sizing for size classes. Having the font sizes for two width options, compact and regular was not a solution for me.
I have written a small lib which handles automatic font scaling for UILabel and UITextView for different screen sizes.
You can set the scaling globally or for a specific instance of UILabel and UITextView.
Find it here: AMXFontAutoScale
I had decent behavior with the following code in my viewDidLoad function:
(Note that the screen bounds are in 'points' not 'pixels')
// Set the clock font size according to the height.
// Design was done on iPhone XR with height of 896 points and font size of 98.
if (UIScreen.main.bounds.height != 896). // Only need code if not on original design size.
{
let scaleFactor: Float = Float(UIScreen.main.bounds.height) / 896.0
let fontSize = CGFloat(98.0 * scaleFactor)
self.clock.font = self.clock.font.withSize(fontSize)
}
This was using the "Helvetica Neue" font which is a fixed width font. It wasn't completely scaled up completely for some reason on larger devices, so still a bit smaller than the target on iPads, but close enough.
different screen sizes acoording to
// iphone 5s,SE screen width 320
// iphone 6,6s,7,etc width 375
// iphone 7 plus, 8 plus, xs max,etc width 414
if (self.view.frame.width == 320) {
label.font = UIFont(name: label.font.fontName, size: 16)
} else if (self.view.frame.width == 375) {
label.font = UIFont(name: label.font.fontName, size: 21)
} else if (self.view.frame.width == 414) {
label.font = UIFont(name: label.font.fontName, size: 24)
}
There is an aspect ratio constraint available. Add this to your label. Constraints to left and top margins for anchoring the label in place should silence the compiler warnings.
As #VatsalManot mentioned, learn adaptive sizing for starters. Here's a good link:
http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial
Hope this helps! :)
for those who want more than xCode editor has to offer this is my hack and its not future proof,
based on this post we can scale font programmatically
Warning!! SCALE FACTOR is not refined yet.
you will need to work on it.
usage:
titleView.font = UIFont.appFont(ofSize: 24, weight: .bold)
contentView.font = UIFont.appFont(ofSize: 16)
code:
extension UIFont {
class func appFont(
ofSize size : CGFloat = UIFont.systemFontSize,
weight : Weight = .regular,
autoScale : Bool = true
) -> UIFont {
return UIFont.systemFont(ofSize: autoScale ? size.dp : size, weight: weight)
}
}
extension CGFloat {
var dp: CGFloat {
let width = UIScreen.main.bounds.width
let device = UIScreen.main.traitCollection.userInterfaceIdiom
if (device == .phone) {
if (width <= 320) {
// iPod(Gen7)
// iPhone(5s, SEGen1)
return self * 0.75
} else if (width <= 375) {
// iPhone(SEGen2 6, 6s, 7, 8, X, Xs, 11pro, 12mini, 13mini)
return self * 0.95
} else if (width <= 414) {
// iPhone(6+, 6s+, 7+, 8+, XsMax, XR, 11, 11proMax, 12, 12pro, 13, 13pro)
return self
} else if (width <= 744) {
// iPhone(12proMax, 13proMax)
return self * 1.2
}
} else if (device == .pad) {
if (width <= 744) {
// ipad(miniGen6, )
return self * 1.4
} else if (width <= 768) {
// ipad(Gen5, Gen6, Air, Air2, Pro9.7)
return self * 1.45
} else if (width <= 810) {
// ipad(Gen9)
return self * 1.5
} else if (width <= 834) {
// ipad(AirGen3, AirGen5, Pro10.5, Pro11Gen1, Pro11Gen3)
return self * 1.55
} else if (width <= 1024) {
// ipad(Pro12.9Gen1, Pro12.9Gen2, Pro12.9Gen3, Pro12.9Gen5)
return self * 1.85
}
}
return self
}
}

Changing font size in a label for only iPhone 4s, is this possible?

I'm working in Portrait mode. I'm looking for a way to change the font size of text in a label so it only applies to the iPhone 4s. I understand that regular height/compact width applies to the iPhone 4s, 5, and 6. But with my app the way it is, I have to make my font size so small just to make it 4s compatible. On the 5 it looks alright, but it makes viewing it on the 6 very very ugly (being of how miniscule it looks). What would you guys do in this situation?
Is there any way to do this programmatically even? If this isn't possible, how could Apple miss this? Doesn't it make more sense to put the 4s in it's own subcategory with regards to it's height?
I am programming in Swift.
Detecting the device model:
if UIDevice.currentDevice().model == "iPhone4,1" {
// do whatever you want here for iPhone 4S
} else {
// other devices
}
nativeBounds
The bounding rectangle of the physical screen, measured in pixels.
(read-only)
Declaration SWIFT
var nativeBounds: CGRect { get }
This rectangle is based on the device in a portrait-up orientation. This
value does not change as the device rotates.
Import Statement
import UIKit
Detecting the device's height:
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
}
Detecting the device's width:
if UIScreen.mainScreen().nativeBounds.width == 640.0 {
}

Resources