0

I want to render text in italics using core text. In UILabel, I was able to apply italics style(basically slanting the text) for a regular font by adding obliqueness attribute to the NSMutableAttributedString. Please refer the code and output below,

let attributes: [NSAttributedString.Key: Any] = 
     .font: UIFont(name: "YesevaOne-Regular", size: 20),
     .obliqueness: 0.3
]
let label = "FontTest"
let attributedString = NSMutableAttributedString(string: label)
attributedString.addAttributes(attributes, range: NSRange.init(location: 0, length: (label as NSString).length))
fontestLabel.attributedText = attributedString

Italics Style applied for uilabel

But, when i do the same in core text, the obliqueness attribute not getting applied to the text, whereas the other attributes like strokewidth, strokecolor was getting applied. I have added the code which i used and output below,

let attrString = NSMutableAttributedString(string: "Font Test")
let attributes: [NSAttributedString.Key: Any] = [
    .backgroundColor : UIColor.white.cgColor,
    .font : UIFont(name: "YesevaOne-Regular", size: 20)!,
    .strokeWidth: -5,
    .obliqueness: 0.3,
]
        
attrString.addAttributes(attributes, range: NSRange(location: 0, length: attrString.length))
        
let framesetter = CTFramesetterCreateWithAttributedString(attrString as CFAttributedString)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil)
        
CTFrameDraw(frame, context)

output for core text

Please help me, if anyone come across this situation?

PS: The custom font, which i used doesn't have italics ttf.

Vimal
  • 29
  • 7
  • 1
    Maybe you can check this: https://stackoverflow.com/questions/9486737/whats-the-coretext-equivalent-to-appkits-nsobliquenessattributename – Moose Nov 15 '22 at 07:55

0 Answers0