CATextLayer字体borderColor? [英] CATextLayer font borderColor?

查看:146
本文介绍了CATextLayer字体borderColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的文字被白色边框包围。我正在使用CATextLayer作为文本。我知道CATextLayer没有属性borderColor / borderWidth。当然,我可以使用其超类(CALayer)的属性,但它会在图层的框架周围绘制边框,而不是文本本身。有人知道如何使用CATextLayer实现这一目标吗?

I would like my text to be surrounded by a white border. I'm using CATextLayer for the text. I know there is no property borderColor/borderWidth for the CATextLayer. Of course I can use the properties of its superclass (CALayer) but then it draws a border around the frame of the layer and not around the text itself. Does anybody know how I could achieve this using CATextLayer?

推荐答案

以防万一有人对我的解决方案感兴趣:

Just in case someone is interested in my solution:

基本上可以使用笔划(边框)创建文本而不直接使用CoreText。 CATextLayer的字符串属性接受NSAttributedStrings。因此,它就像给NSAttributedString的属性中的笔触颜色和笔触宽度一样容易。

Basically it is possible to make text with a stroke (border) without using CoreText directly. The string property of CATextLayer accepts NSAttributedStrings. Therefore it would be as easy as giving a NSAttributedString with a stroke color and a stroke width in its attributes.

不幸的是我需要设置字体大小的动画。 string属性是可动画的,但前提是它是NSString。所以我决定继承CATextLayer。经过多次尝试后,我意识到CATextLayer的字符串和内容属性是互斥的,这意味着要么显示字符串,要么显示内容。我无法弄清楚如何自己绘制字符串。只有在更新内容时才会调用display和drawInContext:ctx方法,但我不知道要更新字符串需要调用什么。

Unfortunately I needed to animated the font size. The string property is animatable but only if it's an NSString. So I decided to subclass CATextLayer. After much trying I came to realize that the string and the contents properties of the CATextLayer are mutually exclusive, which means, either the string or the content is displayed. I couldn't figure out how to do the drawing of the string myself. The display and drawInContext:ctx methods are called only when the content is being updated but I didn't know what I would have to call for updating the string.

所以我决定编写自己的CATextLayer类,继承CA​​Layer。我创建了一个名为fontSize的动画属性。当这个动画时,调用drawInContext:ctx方法。在drawInContext:ctx方法中,我使用CoreText创建一个新字符串,并使用fontSize属性相应地更新其大小。

So I decided to write my own CATextLayer class, subclassing CALayer. I created an animatable property called fontSize. When this one is animated, the drawInContext:ctx method is called. In the drawInContext:ctx method I create a a new string with CoreText and update its size accordingly using the fontSize property.

这篇关于CATextLayer字体borderColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆