TextKit 2 looks great. The video demonstrates the sample app on both MacOS and iPad. However the downloaded code only works on MacOS and the documentation says the NSTextViewportLayoutController is only available on MacOS, so when can we expect the IOS versions. And what about integration with SwiftUI?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
If I call
let font = UIFont.preferredFont(forTextStyle:.body)
Then font.pointSize has value 17
If, however, I call this with a font such as Arial
let font = UIFont.fontWithNameAndTraits(fontName,
size: styleAttributes.fontSize ?? 12,
bold: styleAttributes.bold ?? false,
italic: styleAttributes.italic ?? false)
let fontMetrics = UIFontMetrics(forTextStyle: .body)
let scaledFont = fontMetrics.scaledFont(for: font)
Then scaledFont.pointSize has value 12. I was expecting 17, so I must be doing something wrong. Any suggestion?
The function fontWithNameAndTraits is:
class func fontWithNameAndTraits(_ name:String, size:CGFloat, bold:Bool, italic:Bool)-UIFont {
let fontRef = UIFont.getFontRefForNameAndTraits(name, size:size, bold:bold, italic:italic)
let fontNameKey = CTFontCopyName(fontRef , kCTFontPostScriptNameKey)! as String
return UIFont(name: fontNameKey as String, size:CTFontGetSize(fontRef ))!
}
When I assign an NSMutableAttributedString to a NSAttributedString I am getting error
[UICTFont textBlocks]: unrecognized selector sent to instance
There must be something wrong with the string but I cannot find any documentation that might explain this. Does anyone know what the error means?
Coredata used to save attributed strings, but now requires a secure coding transformer. I have tried the code below, but it fails claiming
This decoder will only decode classes that adopt NSSecureCoding. Class '__SwiftValue' does not adopt it
The transformer has been registered and the attributes given the name 'AttributedStringToDataTransformer'
Any suggestions as to what is wrong?
Here's the code:
class AttributedStringToDataTransformer: ValueTransformer {
override func transformedValue(_ value: Any?) - Any? {
let boxedData = try! NSKeyedArchiver.archivedData(withRootObject: value!, requiringSecureCoding: true)
return boxedData
}
override func reverseTransformedValue(_ value: Any?) - Any? {
let typedBlob = value as! Data
let data = try! NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSAttributedString.self], from: typedBlob)
return (data as! NSAttributedString)
}
}
extension AttributedStringToDataTransformer {
/// The name of the transformer. This is the name used to register the transformer using `ValueTransformer.setValueTrandformer(_"forName:)`.
static let name = NSValueTransformerName(rawValue: String(describing: AttributedStringToDataTransformer.self))
/// Registers the value transformer with `ValueTransformer`.
public static func register() { ValueTransformer.setValueTransformer(AttributedStringToDataTransformer(), forName: name)
}
}
The following code compiles ok in one function:
let stop:UnsafeMutablePointerObjCBool
stop.initialize(to: false)
text.enumerateAttribute(kStyleKey,
in: NSRange(location: 0, length: text.string.count),
options: []) { (value, range, stop) in
but the following code which is virtually the same fails the initialise with the error "Constant 'stop' being used before being initialised" :
let stop:UnsafeMutablePointerObjCBool
stop.initialize(to: false)
attributedText.enumerateAttribute(kStyleKey,
in: NSRange(location: selectedRange.location, length: selectedRange.length),
options: []) { (value, range, stop) in
Any suggestions - looks like a compiler issue to me.
I have been trying to compile and run an example App embedded in a Swift package, but failed. I have extracted the app and tried building it but it can't find the package and nothing I do fixes it. Can someone please let me into the secret.