CATextLayer 忽略字体大小

CATextLayer ignores 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)

新字体是正确的,但它始终与 titleFont 具有相同的大小 (16)。我怎样才能改变字体大小?

在 CATextLayer

font property 上查看此注释

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

清楚地说明了CTFontRef 的字体大小被忽略了。要解决您的问题,您必须显式设置 CATextLayer

fontSize 属性
titleLayer.fontSize = 16
subTitleLayer.fontSize = 10