首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用UILabel同时使用NSAttributedString描边和填充

如何使用UILabel同时使用NSAttributedString描边和填充
EN

Stack Overflow用户
提问于 2013-04-17 06:01:23
回答 5查看 20.6K关注 0票数 37

是否可以使用NSAttributedStringUILabel同时应用笔划fill

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-04-17 06:01:23

是的,关键是将负的值应用于NSStrokeWidthAttributeName,如果此值为正,您将只看到笔触,而不是填充。

Objective-C:

代码语言:javascript
复制
self.label.attributedText=[[NSAttributedString alloc] 
initWithString:@"string to both stroke and fill" 
attributes:@{
             NSStrokeWidthAttributeName: @-3.0,
             NSStrokeColorAttributeName:[UIColor yellowColor],
             NSForegroundColorAttributeName:[UIColor redColor]
             }
];

感谢下面的@cacau :另请参阅Technical Q&A QA1531

Swift 4版本:

代码语言:javascript
复制
let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
                                                 .strokeColor: UIColor.yellow,
                                                 .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)

Swift 5.1版本:

代码语言:javascript
复制
let attributes: [NSAttributedString.Key : Any] = [.strokeWidth: -3.0,
                                                  .strokeColor: UIColor.yellow,
                                                  .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)
票数 91
EN

Stack Overflow用户

发布于 2016-04-27 00:10:50

Swift版本:

代码语言:javascript
复制
let attributes = [NSStrokeWidthAttributeName: -3.0,
                      NSStrokeColorAttributeName: UIColor.yellowColor(),
                      NSForegroundColorAttributeName: UIColor.redColor()];

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)

Swift 3版本:

代码语言:javascript
复制
    let attributes = [NSStrokeWidthAttributeName: -3.0,
                      NSStrokeColorAttributeName: UIColor.yellow,
                      NSForegroundColorAttributeName: UIColor.red] as [String : Any]

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
票数 8
EN

Stack Overflow用户

发布于 2017-11-09 20:05:07

现在,swift apple的最新版本删除了字符串: Any,用于属性键值声明,用作NSAttributedStringKey : Any

代码语言:javascript
复制
    let strokeTextAttributes = [
        NSAttributedStringKey.strokeColor : UIColor.green,
        NSAttributedStringKey.foregroundColor : UIColor.lightGray,
        NSAttributedStringKey.strokeWidth : -4.0,
        NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 52)
        ] as [NSAttributedStringKey : Any]

    hello_cell_lb.attributedText = NSAttributedString(string: "\(hello_array[indexPath.row])", attributes: strokeTextAttributes)

谢谢

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16047901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档