NetNewsWire/Evergreen/MainWindow/Timeline/Cell/SingleLineTextFieldSizer.swift

69 lines
1.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SingleLineTextFieldSizer.swift
// Evergreen
//
// Created by Brent Simmons on 2/19/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
// Get the size of an NSTextField configured with a specific font with a specific size.
// Uses a cache.
// Main thready only.
final class SingleLineTextFieldSizer {
let font: NSFont
private let textField: NSTextField
private var cache = [String: NSSize]()
init(font: NSFont) {
self.textField = NSTextField(labelWithString: "")
self.textField.font = font
self.font = font
}
func size(for text: String) -> NSSize {
if let cachedSize = cache[text] {
return cachedSize
}
textField.stringValue = text
var calculatedSize = textField.fittingSize
calculatedSize.height = ceil(calculatedSize.height)
calculatedSize.width = ceil(calculatedSize.width)
cache[text] = calculatedSize
return calculatedSize
}
static private var sizers = [NSFont: SingleLineTextFieldSizer]()
static func sizer(for font: NSFont) -> SingleLineTextFieldSizer {
if let cachedSizer = sizers[font] {
return cachedSizer
}
let newSizer = SingleLineTextFieldSizer(font: font)
sizers[font] = newSizer
return newSizer
}
// Use this call. Its easiest.
static func size(for text: String, font: NSFont) -> NSSize {
return sizer(for: font).size(for: text)
}
static func emptyCache() {
sizers = [NSFont: SingleLineTextFieldSizer]()
}
}