CATextLayer ignores font-size

4.9k views Asked by At

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?

1

There are 1 answers

1
Zell B. On BEST ANSWER

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