10

I would like to create an overlay of text to my images. The problem is, if ill try to add a second text, like a subtitle it ignores my font-size.

    titleLayer.frame = CGRectMake(0, 80, imageView.bounds.width, 50)
    subTitleLayer.frame = CGRectMake(0, 130, imageView.bounds.width, 40)

    titleLayer.string = "Title"
    subTitleLayer.string = "Subtitle"


    let fontName: CFStringRef = "HelveticaNeue"
    let fontSubName: CFStringRef = "HelveticaNeue-Thin"
    titleLayer.font = CTFontCreateWithName(fontName, 16, nil)
    subTitleLayer.font = CTFontCreateWithName(fontSubName, 10, nil) // Ignores the font-size

    imageView.layer.addSublayer(titleLayer)
    imageView.layer.addSublayer(subTitleLayer)

The new font is correct, but its always with the same size (16) like the titleFont. How can i change the font size?

derdida
  • 14,784
  • 16
  • 90
  • 139

1 Answers1

31

Have a look at this note on font property of CATextLayer

If the font property is a CTFontRef, a CGFontRef, or an instance of NSFont, the font size of the property is ignored.

It clearly explain that font size of CTFontRef is ignored. To solve your problem you have to explicitly set fontSize attribute of CATextLayer

titleLayer.fontSize = 16
subTitleLayer.fontSize = 10
Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • 1
    Thank you very much Zell B. I got this issue the whole afternoon. That interface is really "dangerous" from Apple. It's really slippy, I got caught and never thaught that I must copy the font size into another property... – yves Baumes May 29 '17 at 16:43