Fix serif-font bug and round font in avatar placeholders

This commit is contained in:
Ilya Laktyushin 2019-10-23 15:33:52 +04:00
parent f721a92605
commit 61f71dfcb2
31 changed files with 137 additions and 43 deletions

View File

@ -13,8 +13,6 @@ private func floorToScreenPixels(_ value: CGFloat) -> CGFloat {
return floor(value * UIScreenScale) / UIScreenScale
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 18.0) ?? UIFont.systemFont(ofSize: 18.0)
private let gradientColors: [NSArray] = [
[UIColor(rgb: 0xff516a).cgColor, UIColor(rgb: 0xff885e).cgColor],
[UIColor(rgb: 0xffa85c).cgColor, UIColor(rgb: 0xffcd6a).cgColor],

View File

@ -43,7 +43,7 @@ public class ActionSheetPeerItem: ActionSheetItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 15.0)!
private let avatarFont = avatarPlaceholderFont(size: 15.0)
public class ActionSheetPeerItemNode: ActionSheetItemNode {
private let theme: ActionSheetControllerTheme

View File

@ -69,15 +69,7 @@ NSDictionary *NSAttributedStringAttributesForCoreTextAttributes(NSDictionary *co
// kCTFontAttributeName -> NSFontAttributeName
if ([coreTextKey isEqualToString:(NSString *)kCTFontAttributeName]) {
CTFontRef coreTextFont = (__bridge CTFontRef)coreTextValue;
NSString *fontName = (__bridge_transfer NSString *)CTFontCopyPostScriptName(coreTextFont);
CGFloat fontSize = CTFontGetSize(coreTextFont);
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
ASDisplayNodeCAssertNotNil(font, @"unable to load font %@ with size %f", fontName, fontSize);
if (font == nil) {
// Gracefully fail if we were unable to load the font.
font = [UIFont systemFontOfSize:fontSize];
}
cleanAttributes[NSFontAttributeName] = font;
cleanAttributes[NSFontAttributeName] = (__bridge UIFont *)coreTextFont;
}
// kCTKernAttributeName -> NSKernAttributeName
else if ([coreTextKey isEqualToString:(NSString *)kCTKernAttributeName]) {

View File

@ -14,6 +14,10 @@ private let deletedIcon = UIImage(bundleImageName: "Avatar/DeletedIcon")?.precom
private let savedMessagesIcon = generateTintedImage(image: UIImage(bundleImageName: "Avatar/SavedMessagesIcon"), color: .white)
private let archivedChatsIcon = UIImage(bundleImageName: "Avatar/ArchiveAvatarIcon")?.precomposed()
public func avatarPlaceholderFont(size: CGFloat) -> UIFont {
return Font.with(size: size, design: .round, traits: [.bold])
}
public enum AvatarNodeClipStyle {
case none
case round

View File

@ -174,7 +174,7 @@ class CallListCallItem: ListViewItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
class CallListCallItemNode: ItemListRevealOptionsItemNode {
private let backgroundNode: ASDisplayNode

View File

@ -287,8 +287,6 @@ private final class ChatListItemAccessibilityCustomAction: UIAccessibilityCustom
private let separatorHeight = 1.0 / UIScreen.main.scale
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 26.0)!
private final class CachedChatListSearchResult {
let text: String
let searchQuery: String
@ -433,7 +431,7 @@ class ChatListItemNode: ItemListRevealOptionsItemNode {
self.backgroundNode.isLayerBacked = true
self.backgroundNode.displaysAsynchronously = false
self.avatarNode = AvatarNode(font: avatarFont)
self.avatarNode = AvatarNode(font: avatarPlaceholderFont(size: 26.0))
self.highlightedBackgroundNode = ASDisplayNode()
self.highlightedBackgroundNode.isLayerBacked = true

View File

@ -281,7 +281,7 @@ public class ContactsPeerItem: ListViewItem, ListViewItemWithHeader {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
public class ContactsPeerItemNode: ItemListRevealOptionsItemNode {
private let backgroundNode: ASDisplayNode

View File

@ -16,6 +16,8 @@ public enum DeleteChatPeerAction {
case clearCacheSuggestion
}
private let avatarFont = avatarPlaceholderFont(size: 26.0)
public final class DeleteChatPeerActionSheetItem: ActionSheetItem {
let context: AccountContext
let peer: Peer
@ -41,8 +43,6 @@ public final class DeleteChatPeerActionSheetItem: ActionSheetItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 26.0)!
private final class DeleteChatPeerActionSheetItemNode: ActionSheetItemNode {
private let theme: ActionSheetControllerTheme
private let strings: PresentationStrings

View File

@ -2,6 +2,108 @@ import Foundation
import UIKit
public struct Font {
public enum Design {
case regular
case serif
case monospace
case round
}
public struct Traits: OptionSet {
public var rawValue: Int32
public init(rawValue: Int32) {
self.rawValue = rawValue
}
public init() {
self.rawValue = 0
}
public static let bold = Traits(rawValue: 1 << 0)
public static let italic = Traits(rawValue: 1 << 1)
}
public enum Weight {
case regular
case light
case medium
case semibold
case bold
}
public static func with(size: CGFloat, design: Design = .regular, traits: Traits = []) -> UIFont {
if #available(iOS 13.0, *) {
var descriptor = UIFont.systemFont(ofSize: size).fontDescriptor
var symbolicTraits = descriptor.symbolicTraits
if traits.contains(.bold) {
symbolicTraits.insert(.traitBold)
}
if traits.contains(.italic) {
symbolicTraits.insert(.traitItalic)
}
var updatedDescriptor: UIFontDescriptor? = descriptor.withSymbolicTraits(symbolicTraits)
switch design {
case .serif:
updatedDescriptor = updatedDescriptor?.withDesign(.serif)
case .monospace:
updatedDescriptor = updatedDescriptor?.withDesign(.monospaced)
case .round:
updatedDescriptor = updatedDescriptor?.withDesign(.rounded)
default:
updatedDescriptor = updatedDescriptor?.withDesign(.default)
}
if let updatedDescriptor = updatedDescriptor {
return UIFont(descriptor: updatedDescriptor, size: size)
} else {
return UIFont(descriptor: descriptor, size: size)
}
} else {
switch design {
case .regular:
if traits.contains(.bold) && traits.contains(.italic) {
if let descriptor = UIFont.systemFont(ofSize: size).fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) {
return UIFont(descriptor: descriptor, size: size)
} else {
return UIFont.italicSystemFont(ofSize: size)
}
} else if traits.contains(.bold) {
if #available(iOS 8.2, *) {
return UIFont.boldSystemFont(ofSize: size)
} else {
return CTFontCreateWithName("HelveticaNeue-Bold" as CFString, size, nil)
}
} else if traits.contains(.italic) {
return UIFont.italicSystemFont(ofSize: size)
} else {
return UIFont.systemFont(ofSize: size)
}
case .serif:
if traits.contains(.bold) && traits.contains(.italic) {
return UIFont(name: "Georgia-BoldItalic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else if traits.contains(.bold) {
return UIFont(name: "Georgia-Bold", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else if traits.contains(.italic) {
return UIFont(name: "Georgia-Italic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else {
return UIFont(name: "Georgia", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
case .monospace:
if traits.contains(.bold) && traits.contains(.italic) {
return UIFont(name: "Menlo-BoldItalic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else if traits.contains(.bold) {
return UIFont(name: "Menlo-Bold", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else if traits.contains(.italic) {
return UIFont(name: "Menlo-Italic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
} else {
return UIFont(name: "Menlo", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
case .round:
return UIFont(name: ".SFCompactRounded-Semibold", size: size) ?? UIFont.systemFont(ofSize: size)
}
}
}
public static func regular(_ size: CGFloat) -> UIFont {
return UIFont.systemFont(ofSize: size)
}

View File

@ -233,7 +233,7 @@ public class ItemListAvatarAndNameInfoItem: ListViewItem, ItemListItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 28.0)!
private let avatarFont = avatarPlaceholderFont(size: 28.0)
private let nameFont = Font.medium(19.0)
private let statusFont = Font.regular(15.0)

View File

@ -216,7 +216,7 @@ private let titleBoldFont = Font.medium(17.0)
private let statusFont = Font.regular(14.0)
private let labelFont = Font.regular(13.0)
private let labelDisclosureFont = Font.regular(17.0)
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 15.0)!
private let avatarFont = avatarPlaceholderFont(size: 15.0)
private let badgeFont = Font.regular(15.0)
public class ItemListPeerItemNode: ItemListRevealOptionsItemNode, ItemListItemNode {

View File

@ -11,7 +11,7 @@ import AccountContext
import SelectablePeerNode
import ShareController
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 26.0)!
private let avatarFont = avatarPlaceholderFont(size: 26.0)
private final class MoreNode: ASDisplayNode {
private let avatarNode = AvatarNode(font: Font.regular(24.0))

View File

@ -9,7 +9,7 @@ import TelegramPresentationData
import AvatarNode
import AppBundle
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 24.0)!
private let avatarFont = avatarPlaceholderFont(size: 24.0)
private let avatarBackgroundImage = UIImage(bundleImageName: "Chat/Message/LocationPin")?.precomposed()
private func addPulseAnimations(layer: CALayer) {

View File

@ -4,7 +4,7 @@ import AsyncDisplayKit
import Display
import TelegramPresentationData
private let textFont: UIFont = UIFont(name: ".SFCompactRounded-Semibold", size: 13.0)!
private let textFont = Font.with(size: 13.0, design: .round, traits: [.bold])
private class ChatMessageLiveLocationTimerNodeParams: NSObject {
let backgroundColor: UIColor

View File

@ -11,7 +11,7 @@ import TelegramUIPreferences
import AvatarNode
import AppBundle
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 26.0)!
private let avatarFont = avatarPlaceholderFont(size: 26.0)
private let titleFont = Font.semibold(14.0)
private let textFont = Font.regular(14.0)

View File

@ -34,7 +34,7 @@ final class ChannelDiscussionGroupActionSheetItem: ActionSheetItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 26.0)!
private let avatarFont = avatarPlaceholderFont(size: 26.0)
private final class ChannelDiscussionGroupActionSheetItemNode: ActionSheetItemNode {
private let theme: ActionSheetControllerTheme

View File

@ -13,7 +13,7 @@ import LegacyComponents
import ContextUI
import LocalizedPeerData
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 24.0)!
private let avatarFont = avatarPlaceholderFont(size: 24.0)
private let textFont = Font.regular(11.0)
public final class SelectablePeerNodeTheme {

View File

@ -99,7 +99,7 @@ final class ItemListWebsiteItem: ListViewItem, ItemListItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 9.0)!
private let avatarFont = avatarPlaceholderFont(size: 9.0)
private let titleFont = Font.medium(15.0)
private let textFont = Font.regular(13.0)

View File

@ -38,7 +38,7 @@ import PhoneNumberFormat
private let maximumNumberOfAccounts = 3
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 13.0)!
private let avatarFont = avatarPlaceholderFont(size: 13.0)
private final class ContextControllerContentSourceImpl: ContextControllerContentSource {
let controller: ViewController

View File

@ -11,7 +11,7 @@ import AccountContext
import LocalizedPeerData
private let animationDurationFactor: Double = 1.0
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
private protocol AbstractSwitchAccountItemNode {
func updateLayout(maxWidth: CGFloat) -> (CGFloat, CGFloat, (CGFloat) -> Void)

View File

@ -60,7 +60,7 @@ private struct ShareGridTransaction {
let animated: Bool
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 17.0)!
private let avatarFont = avatarPlaceholderFont(size: 17.0)
private func preparedGridEntryTransition(account: Account, from fromEntries: [SharePeerEntry], to toEntries: [SharePeerEntry], interfaceInteraction: ShareControllerInteraction) -> ShareGridTransaction {
let (deleteIndices, indicesAndItems, updateIndices) = mergeListsStableWithUpdates(leftList: fromEntries, rightList: toEntries)

View File

@ -45,7 +45,7 @@ public class LocationBroadcastActionSheetItem: ActionSheetItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 15.0)!
private let avatarFont = avatarPlaceholderFont(size: 15.0)
public class LocationBroadcastActionSheetItemNode: ActionSheetItemNode {
private let theme: ActionSheetControllerTheme

View File

@ -5,8 +5,8 @@ import Display
import AvatarNode
import ContextUI
private let normalFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let smallFont = UIFont(name: ".SFCompactRounded-Semibold", size: 12.0)!
private let normalFont = avatarPlaceholderFont(size: 16.0)
private let smallFont = avatarPlaceholderFont(size: 12.0)
final class ChatAvatarNavigationNodeView: UIView, PreviewingHostView {
var previewingDelegate: PreviewingHostViewDelegate? {

View File

@ -60,7 +60,7 @@ final class ChatMediaInputPeerSpecificItem: ListViewItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 12.0)!
private let avatarFont = avatarPlaceholderFont(size: 12.0)
private let boundingSize = CGSize(width: 41.0, height: 41.0)
private let boundingImageSize = CGSize(width: 28.0, height: 28.0)
private let highlightSize = CGSize(width: 35.0, height: 35.0)

View File

@ -8,7 +8,7 @@ import TelegramPresentationData
import AvatarNode
import AccountContext
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
final class ChatMessageAvatarAccessoryItem: ListViewAccessoryItem {
private let context: AccountContext

View File

@ -11,7 +11,7 @@ import AvatarNode
import AccountContext
import PhoneNumberFormat
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
private let titleFont = Font.medium(14.0)
private let textFont = Font.regular(14.0)

View File

@ -57,8 +57,8 @@ public final class ChatMessageNotificationItem: NotificationItem {
}
}
private let compactAvatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 20.0)!
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 24.0)!
private let compactAvatarFont = avatarPlaceholderFont(size: 20.0)
private let avatarFont = avatarPlaceholderFont(size: 24.0)
final class ChatMessageNotificationItemNode: NotificationItemNode {
private var item: ChatMessageNotificationItem?

View File

@ -76,7 +76,7 @@ final class CommandChatInputPanelItem: ListViewItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
private let textFont = Font.medium(14.0)
private let descriptionFont = Font.regular(14.0)

View File

@ -78,7 +78,7 @@ final class MentionChatInputPanelItem: ListViewItem {
}
}
private let avatarFont = UIFont(name: ".SFCompactRounded-Semibold", size: 16.0)!
private let avatarFont = avatarPlaceholderFont(size: 16.0)
private let primaryFont = Font.medium(14.0)
private let secondaryFont = Font.regular(14.0)

View File

@ -12,7 +12,7 @@ final class WebSearchBadgeNode: ASDisplayNode {
private let textNode: ASTextNode
private let backgroundNode: ASImageNode
private let font: UIFont = UIFont(name: ".SFCompactRounded-Semibold", size: 17.0)!
private let font: UIFont = Font.with(size: 17.0, design: .round, traits: [.bold])
var text: String = "" {
didSet {

@ -1 +1 @@
Subproject commit 699d822fc547e6d9b997667f8d5aecf75182d0a3
Subproject commit a09896b3e72e76681c12e80572a7d570108cf885