gpt4 book ai didi

ios - 如何在 UITextView 中为文本添加轮廓?

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:20 24 4
gpt4 key购买 nike

我是 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

最佳答案

您需要阅读 NSAttributedString。你可以给字母一个轮廓笔画:

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

使用 NSStrokeColorAttributeNameNSStrokeWidthAttributeName 配置轮廓笔触。

一旦你有了你的属性字符串,你就可以把它放在一个标签中,把它放在一个 TextView 中,或者直接绘制它,随你喜欢。

enter image description here

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

enter image description here

关于ios - 如何在 UITextView 中为文本添加轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006404/

24 4 0
文章推荐: HTML 列表仅首行缩进
文章推荐: php - PHP 使用的 OS X 和 OpenSSL 上的 Composer 错误
文章推荐: html - 使用
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com