CATextLayer ignores font size

I would like to create text overlay on my images. The problem is that if you try to add a second text, such as 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 it is always the same size (16) as the titleFont. How to change the font size?

+6
source share
1 answer

Take a look at this note on the font property in CATextLayer

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

Clearly, the font size of CTFontRef is ignored. To solve your problem, you must explicitly set the fontSize attribute for CATextLayer

 titleLayer.fontSize = 16 subTitleLayer.fontSize = 10 
+10
source

Source: https://habr.com/ru/post/989731/


All Articles