UITextView font to always be fixed size

2020-02-15 09:02发布

问题:

I have a UICollectionView with cells sized to the same aspect ratio as a4 page. And with different screen sizes I would like for that text to be sized at fixed 12pt (that would be calculated to that UICollectionview cell), which would look bigger on the iPad and smaller on iPhone.

So having a fixed font size isn't an option...

回答1:

Coding

var fontSize : CGFloat = 12.0 // DEFAULT SIZE

In your ViewDidLoad

if UIDevice().userInterfaceIdiom == .phone {
    switch self.view.frame.size.height {
    case 480:
        fontSize = 12.0
    case 568:
        fontSize = 14.0
    case 667:
        fontSize = 16.0
    case 736:
        fontSize = 17.0
    case 812:
        fontSize = 18.0
    default:
        print("unknown")
    }
}
else if UIDevice().userInterfaceIdiom == .pad {

    fontSize = 20.0

}

Then apply this fontSize, wherever u need.

Storyboard

Select UITextView, in Attributes Inspector area, + (plus) symbol is near to Font. By default it will work for iPhone devices. For iPad, Change change size classes to Regular width and Regular Height wR hR . For, that U can increase font size.