Reputation: 111
I'm trying to draw text using CoreText and CoreGraphics. The text draws and positions fine, however I can't seem to be able to change font size. Or font for that matter. I tried setting attributed text with CFAttributedString directly and I tried changing the CGContext font size setting. Here's my code
//... (create CGContext)
let textAttrs = [
kCTFontSizeAttribute as String : 18 as NSNumber
] as CFDictionary
let attrString = CFAttributedStringCreate(kCFAllocatorDefault,
NSString(string: "Hello, world!"),
textAttrs)
let textLine = CTLineCreateWithAttributedString(attrString!)
context.textPosition = CGPoint.zero
CTLineDraw(textLine, context.textPosition)
Upvotes: 2
Views: 403
Reputation: 111
The problem is with the definition of attributes. First I had to define font and then apply font as attribute for string.
let fontAttributes = [
kCTFontFamilyNameAttribute : "Courier",
kCTFontStyleNameAttribute : "Bold",
kCTFontSizeAttribute : 18.0
] as NSDictionary
let descriptor = CTFontDescriptorCreateWithAttributes(fontAttributes)
let font = CTFontCreateWithFontDescriptor(descriptor, 0.0, nil)
let attributes = [kCTFontAttributeName : font] as CFDictionary
let attrString = CFAttributedStringCreate(kCFAllocatorDefault,
"Hello, world!" as NSString,
attributes)
Upvotes: 1