Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CATextLayer ignores font-size

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?

like image 447
derdida Avatar asked Jun 25 '15 09:06

derdida


1 Answers

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
like image 112
Zell B. Avatar answered Oct 23 '22 19:10

Zell B.