10

我想为我的图像创建一个文本覆盖。问题是,如果我尝试添加第二个文本,比如字幕,它会忽略我的字体大小。

    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)。如何更改字体大小?

4

1 回答 1

29

看看这个关于CATextLayer字体属性的注释

如果字体属性是 CTFontRef、CGFontRef 或 NSFont 的实例,则忽略该属性的字体大小。

它清楚地解释了 CTFontRef 的字体大小被忽略。要解决您的问题,您必须明确设置fontSizeCATextLayer 的属性

titleLayer.fontSize = 16
subTitleLayer.fontSize = 10
于 2015-06-25T09:21:53.713 回答