如何在UITextView中为文本添加大纲?

时间:2015-01-18 01:54:56

标签: ios swift text

我是iOS中文本编程的新手,目前有点难过。我正在创建一个示例项目来学习如何制作一个lolcat应用程序(从有趣的事情开始!)。

我和猫有了一个UIImage,我通过在文本顶部放置UITextView来在其上面放置一些文本。

我想在文本中结束这样的事情:

lolcat

当我在UILabel中输入文本时,我能够成功地将其关闭。我使用了我在SO上找到的使用drawTextInRect来实现魔法的代码。

现在,我想在UITextView中做同样的事情(我希望能够滚动文本)。

**编辑:**所以这是我到目前为止所做的:

var foo = NSAttributedString(string: text, attributes: [
        NSStrokeColorAttributeName : UIColor.blackColor(),
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSStrokeWidthAttributeName : NSNumber(float: -4.0),
        NSFontAttributeName : UIFont.systemFontOfSize(30.0)
 ])

这给我一个这样的结果:

enter image description here

如果我现在将笔划宽度加倍到-8.0,我会得到以下结果,这在我的估计中看起来非常糟糕。似乎在笔划宽度和我可以看到的填充量之间存在折衷。理想情况下,我喜欢在没有吃掉白色文字的情况下让笔触变宽。

enter image description here

2 个答案:

答案 0 :(得分:3)

您需要阅读NSAttributedString。你可以给字母画一个大纲:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/#//apple_ref/doc/constant_group/Character_Attributes

使用NSStrokeColorAttributeNameNSStrokeWidthAttributeName配置大纲描边。

获得属性字符串后,您可以将其添加为标签,将其放在文本视图中,或者直接绘制它。

enter image description here

如果添加一些扩展和一点点阴影,可能看起来好一点:

enter image description here

答案 1 :(得分:2)

您可以使用库DrawingLabel的帮助,而无需任何CoreGraphics代码

DrawingStroke *blackStroke = [DrawingStroke new];
blackStroke.strokeWidth = 3.0;
blackStroke.strokeColor = [UIColor blackColor];