Change font size according to iOS device

0

I have an App of sentences and each sentence has a different size (according to the amount of words) and in tablets as the resolution is the same as the iphone 4s the sentences extrapolate the size reserved for them and are behind a button that I have on the screen (centered at the bottom of the app), not displaying its entire content. Is it possible to change the font size according to the type of device? Or what would be the best solution to solve such a problem? It is worth mentioning that in iPhones this text is well away from the button which makes it unfeasible to always lower the text or throw it up.

Ihavetriedtocreateavariation,butitdoesnotseemtosolve:

    
asked by anonymous 08.11.2017 / 10:52

3 answers

1

There is a solution, if you call "Auto Layout" you should select the text and in the xcode inspector you will trigger a rule for it by clicking on the + button on the font.

Seewhatfieldsappearforyoutochoosethewidthandheightforthenewvariation.thesizesyoucancheckbelow:

This way you just have to select the option you want and see how it looks in your app.

    
06.12.2017 / 14:20
0

I think it's possible to adjust with Auto Layout , as was suggested in another answer.

For this, it would be nice to make sure that bottomAnchor of Label is anchored to topAnchor of the button, preventing it from going behind it.

Then, to show all lines of Label and ensure that they fit in the space it has:

label.numberOfLines = 0;

// Faz com que a fonte reduza para caber no container
label.adjustsFontSizeToFitWidth = true;

If you want to prevent the font from reducing too much, you can set how much of the original size it can reduce:

// Permite reduzir até metade do tamanho original da fonte da label    
label.minimumScaleFactor = 0.5

Q.: Thinking about very long sentence cases, there are some alternatives to work around, such as using a non-editable / selectable UITextView .

I hope I have helped!

    
20.02.2018 / 20:03
0

Dear friend, the idea is that autoLayout really helps you, but as your question was to know which type of device, the answer follows:

You can determine the device as follows:

To facilitate I will create 2 structs.

This first dynamically takes the width and height of the device, determining the max and min:

    public struct ScreenSize{
      static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
      static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
      static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
      static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    }

This second already has the rules to determine the type of device used:

    public struct DeviceType
    {
      static let IS_IPHONE_4_OR_LESS =  UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
      static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
      static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
      static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
      static let IS_IPHONE_X = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.0
    }

An example of its use would be:

    if DeviceType.IS_IPHONE_4_OR_LESS {          
                // 4S ou anterior          
    } 

Static constants have been created to make it easier to use in your classes.

    
09.05.2018 / 21:45