也许不会,你可能在你的控件上设置了字体,但是你可以通过集中从哪里获得字体类型来简化这个过程,例如有应用程序委托或其他一些常见的类有一个返回字体的方法,任何需要设置字体的东西都可以调用这个方法,这将有助于在你需要改变你的字体的情况下,你可以在一个地方改变它,而不是在你设置字体的任何地方... 另一种选择可以是你的 UI Element 的子类,它会自动设置字体,但是这可能有点过了。.
class ATFontManager: UIFont{
class func setFont( _ iPhone7PlusFontSize: CGFloat? = nil,andFontName fontN : String = FontName.helveticaNeue) -> UIFont{
let defaultFontSize : CGFloat = 16
switch ATDeviceDetector().screenType {
case .iPhone4:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 5)!
}
return UIFont(name: fontN, size: defaultFontSize - 5)!
case .iPhone5:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 3)!
}
return UIFont(name: fontN, size: defaultFontSize - 3)!
case .iPhone6AndIphone7, .iPhoneUnknownSmallSize:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 2)!
}
return UIFont(name: fontN, size: defaultFontSize - 2)!
case .iPhone6PAndIPhone7P, .iPhoneUnknownBigSize:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? defaultFontSize)!
case .iPhoneX, .iPhoneXsMax:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? defaultFontSize)!
case .iPadMini:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 2)!
}
return UIFont(name: fontN, size: defaultFontSize + 2)!
case .iPadPro10Inch:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 4)!
}
return UIFont(name: fontN, size: defaultFontSize + 4)!
case .iPadPro:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 6)!
}
return UIFont(name: fontN, size: defaultFontSize + 6)!
case .iPadUnknownSmallSize:
return UIFont(name: fontN, size: defaultFontSize + 2)!
case .iPadUnknownBigSize:
return UIFont(name: fontN, size: defaultFontSize + 4)!
default:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? 16)!
}
}
}
我已经添加了一些字体名称,更多的你可以添加字体名称和类型在这里。
enum FontName : String {
case HelveticaNeue = "HelveticaNeue"
case HelveticaNeueUltraLight = "HelveticaNeue-UltraLight"
case HelveticaNeueBold = "HelveticaNeue-Bold"
case HelveticaNeueBoldItalic = "HelveticaNeue-BoldItalic"
case HelveticaNeueMedium = "HelveticaNeue-Medium"
case AvenirBlack = "Avenir-Black"
case ArialBoldMT = "Arial-BoldMT"
case HoeflerTextBlack = "HoeflerText-Black"
case AMCAPEternal = "AMCAPEternal"
}
此类指设备检测器,以便根据设备提供适当的字体大小。
class ATDeviceDetector {
var iPhone: Bool {
return UIDevice().userInterfaceIdiom == .phone
}
var ipad : Bool{
return UIDevice().userInterfaceIdiom == .pad
}
let isRetina = UIScreen.main.scale >= 2.0
enum ScreenType: String {
case iPhone4
case iPhone5
case iPhone6AndIphone7
case iPhone6PAndIPhone7P
case iPhoneX
case iPadMini
case iPadPro
case iPadPro10Inch
case iPhoneOrIPadSmallSizeUnknown
case iPadUnknown
case unknown
}
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)
}
var screenType: ScreenType {
switch ScreenSize.SCREEN_MAX_LENGTH {
case 0..<568.0:
return .iPhone4
case 568.0:
return .iPhone5
case 667.0:
return .iPhone6AndIphone7
case 736.0:
return .iPhone6PAndIPhone7P
case 812.0:
return .iPhoneX
case 568.0..<812.0:
return .iPhoneOrIPadSmallSizeUnknown
case 1112.0:
return .iPadPro10Inch
case 1024.0:
return .iPadMini
case 1366.0:
return .iPadPro
case 812.0..<1366.0:
return .iPadUnknown
default:
return .unknown
}
}
}
如何使用。希望对你有帮助。
//for default
label.font = ATFontManager.setFont()
//if you want to provide as your demand. Here **iPhone7PlusFontSize** variable is denoted as font size for *iphone 7plus and iphone 6 plus*, and it **ATFontManager** class automatically handle.
label.font = ATFontManager.setFont(iPhone7PlusFontSize: 15, andFontName: FontName.HelveticaNeue.rawValue)