Chcę obsługiwać zarówno system iOS 8, jak i iOS 9 dla mojej aplikacji. A może iOS 7. Jak wiemy, czcionka systemowa dla iOS 7 i 8 to Helvetica Neue. Ale w systemie iOS 9 czcionka systemu to San Francisco. A jeśli nie ustawisz czcionki Helvetica jawnie przez [UIFont fontWithName:@"HelveticaNeue" size:15];
, ale użyjesz [UIFont systemFontOfSize:15];
, automatycznie otrzymasz Helvetica dla iOS 7 i 8 oraz San-Francisco dla iOS 9. I jest wspaniale! W przypadku etykiet i przycisków konstruktora interfejsu można ustawić cienkie, ultra cienkie, średnie itp. Czcionki systemowe. To też jest świetne. Ale w jaki sposób programowo ustawić te cienkie, ultra, średnie systemowe czcionki w kodzie? Czy muszę utworzyć kategorię z widelcem dla systemu iOS 9 i poprzedniego systemu iOS?Czcionka systemowa dla systemu iOS 8 i iOS 9
Odpowiedz
Użyj + systemFontOfSize:weight:
. Jest dostępny dla systemu iOS 8 i nowszych wersji.
W przypadku iOS 7 ustawienia kreatora interfejsu będą działać, a dla kodu należy utworzyć UIFontDescriptor
o odpowiedniej wadze.
Dzięki @Leo Natan. Ale chcę pokazać fragment kodu dla miłośników kopiowania i wklejania.
UIFont* systemFont = [UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] ? [UIFont systemFontOfSize:25 weight:UIFontWeightThin] : [UIFont fontWithName:@"HelveticaNeue-Thin" size:25];
Lepiej jest testować pod kątem określonej funkcjonalności niż wersji systemu operacyjnego. Użyj '[UIFont respondsToSelector: @selector (systemFontOfSize: weight :)]', aby przetestować dostępność. –
@LeoNatan Masz rację! Oczywiście –
Utworzyłem tego rozszerzenia:
import Foundation
import UIKit
enum SystemFontWeight : String {
case UltraLight = "HelveticaNeue-UltraLight"
case Thin = "HelveticaNeue-Thin"
case Light = "HelveticaNeue-Light"
case Regular = "HelveticaNeue"
case Medium = "HelveticaNeue-Medium"
case Semibold = "Helvetica-Bold"
case Bold = "HelveticaNeue-Bold"
case Heavy = "HelveticaNeue-CondensedBold"
case Black = "HelveticaNeue-CondensedBlack"
var weightValue:CGFloat? {
if #available(iOS 8.2, *) {
switch self {
case .UltraLight:
return UIFontWeightUltraLight
case .Thin:
return UIFontWeightThin
case .Light:
return UIFontWeightLight
case .Regular:
return UIFontWeightRegular
case .Medium:
return UIFontWeightMedium
case .Semibold:
return UIFontWeightSemibold
case .Bold:
return UIFontWeightBold
case .Heavy:
return UIFontWeightHeavy
case .Black:
return UIFontWeightBlack
}
} else {
return nil
}
}
}
extension UIFont {
static func systemFontOfSize(fontSize:CGFloat, weight:SystemFontWeight) -> UIFont {
if #available(iOS 8.2, *) {
return UIFont.systemFontOfSize(fontSize, weight: weight.weightValue!)
} else {
// Fallback on earlier versions
return UIFont(name: weight.rawValue, size: fontSize)!
}
}
}
co sprawia, że można zastosować czcionkę tak:
myLabel.font = UIFont.systemFontOfSize(14, weight: .Medium)
To automatycznie ustawić właściwą czcionkę zarówno dla iOS 8 i iOS 9.
Dzięki @Antoine za opublikowanie wspaniałej odpowiedzi na szybkie. Poniżej znajduje się Cel C Podobny rodzaj odpowiedzi, jeśli ktoś chce. Wdrożenie kategorię UIFont
UIFont + Cat.m
#import "UIFont+Cat.h"
@implementation UIFont (Cat)
+ (UIFont *)systemFontWithSize:(CGFloat)fontSize weight:(CGFloat)weight {
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
return [UIFont systemFontOfSize:fontSize weight:weight];
}
NSString *fontName = @"HelveticaNeue";
if (weight == UIFontWeightUltraLight) {
fontName = @"HelveticaNeue-UltraLight";
}
else if (weight == UIFontWeightThin) {
fontName = @"HelveticaNeue-Thin";
}
else if (weight == UIFontWeightLight) {
fontName = @"HelveticaNeue-Light";
}
else if (weight == UIFontWeightRegular) {
fontName = @"HelveticaNeue";
}
else if (weight == UIFontWeightMedium) {
fontName = @"HelveticaNeue-Medium";
}
else if (weight == UIFontWeightSemibold) {
fontName = @"Helvetica-Bold";
}
else if (weight == UIFontWeightBold) {
fontName = @"HelveticaNeue-Bold";
}
else if (weight == UIFontWeightHeavy) {
fontName = @"HelveticaNeue-CondensedBold";
}
else if (weight == UIFontWeightBlack) {
fontName = @"HelveticaNeue-CondensedBlack";
}
return [UIFont fontWithName:fontName size:fontSize];
}
@end
- 1. Co to jest czcionka systemu iOS 8?
- 2. Niestandardowe odświeżanie dla systemu iOS 8 i iOS 9
- 3. Roboto czcionka dla ios
- 4. Czcionka OpenSans nie działa w symulatorze iOS.
- 5. Alternatywa dla UIAlertView dla iOS 9?
- 6. GIDSignIn iOS 9
- 7. Kod iOS Działa na iOS 9, ale nie na iOS 8?
- 8. Wersja aplikacji dla systemu iOS
- 9. Alternatywa AudioContext.createMediaStreamSource dla systemu iOS?
- 10. IOS 9 NSURLConnection przestarzałe
- 11. inny system Średnia czcionka na iOS 7
- 12. iOS 9 ATS i Firebase REST
- 13. iOS 9 UIButton Inititalization
- 14. Odpowiednik iOS dla systemu Android()
- 15. Szyfrowanie systemu plików dla IOS?
- 16. Swift 3 IOS 9 i IOS 10 Core Data
- 17. iOS 9 Breaks animacje UILabel
- 18. GIDSignIn biały ekran na iOS 9
- 19. iOS 8 NSInternalInconsistencyException
- 20. Safari nie jest otwarte aplikacje na iOS w iOS 9
- 21. Stan WKWebView na Cordova iOS 9?
- 22. Niestandardowa czcionka z klasami rozmiarów w iOS
- 23. iOS 9 - Zasoby reklamowe niedostępna
- 24. Dokładna czcionka Quran z Thajweed - ios
- 25. iOS 9 Przedsiębiorstwo zweryfikować udało
- 26. MapKit iOS 9 detailCalloutAccessoryView use
- 27. Uniwersalny link w iOS 8?
- 28. UIAlertController nie działa w systemie iOS 9
- 29. RESTful frameworks dla systemu Android, iOS ...?
- 30. ptrace na iOS 8
'+ systemFontOfSize: waga:' Dostępne w iOS 8.2 i nowszym. –
@ValentinShamardin Nie martw się o to, metoda była tam od iOS 8. Została ujawniona jako publiczny API w wersji 8.2, ale możesz go użyć. –