UIFont' is not convertible to 'UIFont?'
Elsewhere on SO, someone suggest that where you have this:
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)
...you should try writing this:
aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)
I can take no credit for this (because I can't reproduce the bug) so I'm just guessing! But it would be very interesting to know if it actually works.
UIFont is not convertible error
placeholder.font = UIFont(name: "Avenir-Light", size: 15.0)
you should try writing this:
placeholder.font = UIFont.init(name: "Avenir-Light", size: 15)
for more information see this
How to convert CGFontRef to UIFont?
Conrad's answer is close but doesn't quite work. You need to provide UIFont with the PostScript name, rather than the full name.
NSString *fontName = (NSString *)CGFontCopyPostScriptName(fontRef);
UIFont *font = [UIFont fontWithName:fontName size:someSize]
Convert CATextLayer font to UIFont
I just wrote the following extension which should handle: CTFont
, CGFont
, string name or UIFont
:
extension CATextLayer {
public var typeFace:UIFont? {
let name:String
switch font {
case let ctFont as CTFont :
// If you assign a UIFont to CATextLayer.font
// it is implicitly converted to CTFont,
// so this case will be called.
guard let _name = CTFontCopyName(ctFont, kCTFontPostScriptNameKey) else {
return nil
}
name = _name as NSString as String
case let cgFont as CGFont :
guard let _name = cgFont.postScriptName else {
return nil
}
name = _name as NSString as String
case let string as NSString :
name = string as String
case let string as String :
name = string
default:
return nil
}
return UIFont(name:name, size:fontSize)
}
}
On a side note, I found this comment in docs inaccurate:
In iOS, you cannot assign a UIFont object to this property.
From my tests I was able to set a UIFont
on CATextLayer.font
and it appears to work fine.
EDIT
It turns out when you assign a UIFont
to CATextLayer.font
it is converted to a CTFont
behind the scenes, so in order to get the UIFont
back out it's necessary to convert from CTFont
to UIFont
(which my code does). Simply casting to UIFont
would not work.
SwiftUI/UIKit How to convert SwiftUI Font to UIKit UIFont?
If you need share same font, then use UIFont
as model property and convert in SwiftUI where needed as, for example
Text("Some text")
.font(Font(uiFont as CTFont))
iPhone - Convert CTFont to UIFont?
CTFontRef ctFont = ...;
NSString *fontName = [(NSString *)CTFontCopyName(ctFont, kCTFontPostScriptNameKey) autorelease];
CGFloat fontSize = CTFontGetSize(ctFont);
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
Color and underline are not attributes of the font. Bold and italic are part of the font name.
Related Topics
Swift Dictionary Default Value
Load Image from iOS 8 Framework
How to Animate a Model's Rotation in Realitykit
Uitableviewautomaticdimension Not Working for Resizing Cell Height
Iboutlet of Another View Controller Is Nil
Using Combine's Future to Replicate Async Await in Swift
Embedding Videos in a Tableview Cell
Convert Firebase Firestore Timestamp to Date (Swift)
How to Convert Range in Nsrange
Swift: Extending Functionality of Print() Function
How to Make an Array of the Current Week Dates Swift
How to Create a PDF in Swift with Cocoa (Mac)
Swift 3: Set Finder Label Color
How to Have Two Alerts on One View in Swiftui
Swift 3 Error:Argument Labels '(_:)' Do Not Match Any Available Overloads