我正在尝试使用CoreText和CoreGraphics绘制文本。文字绘制和位置很好,但是我似乎无法更改字体大小。还是字体。我尝试直接使用CFAttributedString设置属性文本,并尝试更改CGContext字体大小设置。这是我的代码
//... (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)
答案 0 :(得分:0)
问题在于属性的定义。首先,我必须定义字体,然后将字体用作字符串的属性。
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)