加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – CATextLayer忽略font-size

发布时间:2020-12-14 05:27:57 所属栏目:百科 来源:网络整理
导读:我想为我的图像创建一个文本叠加层.问题是,如果生病了尝试添加第二个文本,如字幕,它会忽略我的字体大小. titleLayer.frame = CGRectMake(0,80,imageView.bounds.width,50) subTitleLayer.frame = CGRectMake(0,130,40) titleLayer.string = "Title" subTitle
我想为我的图像创建一个文本叠加层.问题是,如果生病了尝试添加第二个文本,如字幕,它会忽略我的字体大小.
titleLayer.frame = CGRectMake(0,80,imageView.bounds.width,50)
    subTitleLayer.frame = CGRectMake(0,130,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读