This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension Image { | |
init(symbol name: String, accessibilityDescription: String? = nil) { | |
if let nsImage = NSImage(systemSymbolName: name, accessibilityDescription: accessibilityDescription) { | |
self.init(nsImage: nsImage) | |
} else if let nsImage = NSImage(named: name) { | |
nsImage.accessibilityDescription = accessibilityDescription | |
self.init(nsImage: nsImage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
16 November 2021 | |
SwiftUI.TextField used with NumberFormatter() as a formatter does not set/update value on macOS 12 (Monterey). This is regression as it works ok on macOS 11 (BigSur) | |
Please find attached Playground code and code screenshot with marked bug | |
Related FB9180913. Reported June 15. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextFormatter: Formatter { | |
override func string(for obj: Any?) -> String? { | |
obj as? String | |
} | |
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool { | |
obj?.pointee = string as AnyObject | |
return true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file defines two numbers to use to mean "really big". | |
// The first is called LargeNumberForText, and it was not arbitrarily chosen. | |
// The actual value was chosen to be around the largest floating point value possible that can preserve at least pixel precision. | |
// Because of the nature of floating point numbers, the bigger they get, the less precise they get. | |
// It is not wise to use bigger dimensions for text system objects because, even if you ever fill all that space, | |
// by the time you get to the far reaches, the letters won't have the precision necessary to look and act correctly. | |
// This limitation of floating point coordinates goes all the way down into postscript, and holds for any type postscript graphics. | |
// Because the Cocoa text system respects this limit in various ways, a second constant, NotQuiteAsLargeNumberForText, | |
// is used for the field-like text views created by the FieldAspect class. This is simply half of LargeNumberForText; | |
// at sizes as large as LargeNumberForText, the text s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let formatter = PersonNameComponentsFormatter() | |
formatter.style = .long | |
do { | |
let components = formatter.personNameComponents(from: "Marcin Krzyżanowski")! | |
print(components.givenName!) // Marcin | |
print(components.familyName!) // Krzyżanowski | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop | |
// Licence BSD-2 clause | |
// Marcin Krzyzanowski marcin@krzyzanowskim.com | |
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize { | |
let framesetter = CTFramesetterCreateWithAttributedString(attributedString) | |
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000)) | |
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "dupa\r\nblada" | |
for i in str.indices { | |
switch str[i] { | |
case "\n": | |
print("\\n found") // \n not found | |
case "\r": | |
print("\\r found") // \r not found | |
case "\r\n": | |
print("\\r\\n found") // found | |
default: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/* | |
D | |
O | |
O | |
G R | |
S | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bom: String = "\u{feff}" // The character \uFEFF is the BOM character for all UTFs (8, 16 LE and 16 BE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "abc😂" | |
str.count // 4 | |
str.utf16.count // 5 | |
str.utf8.count // 7 | |
import Foundation | |
(str as NSString).length // 5 | |
(str as NSString).lengthOfBytes(using: String.Encoding.utf8.rawValue) // 7 |