首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAttributedString不遵守foregroundColor

NSAttributedString不遵守foregroundColor
EN

Stack Overflow用户
提问于 2020-11-27 03:00:50
回答 1查看 24关注 0票数 0

我有一个使用NSAttributedString设置给定文本笔画的UIViewRepresentable

它工作得很好,除了一件事:文本没有填充--只有笔画。

代码语言:javascript
复制
struct StrokedText: UIViewRepresentable {
    let text: String
    let width: CGFloat
    let color: Color
    let fontSize: CGFloat
    
    func makeUIView(context: Context) -> UILabel {
        let attributedStringParagraphStyle = NSMutableParagraphStyle()
        attributedStringParagraphStyle.alignment = NSTextAlignment.center
        let attributedString = NSAttributedString(
            string: text,
            attributes:[
                NSAttributedString.Key.paragraphStyle: attributedStringParagraphStyle,
                NSAttributedString.Key.strokeWidth: fontSize * 0.05,
                NSAttributedString.Key.foregroundColor: UIColor.white,
                NSAttributedString.Key.strokeColor: UIColor(color),
                NSAttributedString.Key.font: UIFont(name:"Coiny-Regular", size: fontSize)!
            ]
        )

        let strokeLabel = UILabel(frame: CGRect.zero)
        strokeLabel.attributedText = attributedString
        strokeLabel.backgroundColor = UIColor.clear
        strokeLabel.sizeToFit()
        strokeLabel.center = CGPoint.init(x: 0.0, y: 0.0)
        return strokeLabel
    }

    func updateUIView(_ uiView: UILabel, context: Context) {}
}

用法:

代码语言:javascript
复制
struct MyView: View {
   var body: some View {
     GeometryReader { geo in
        StrokedText(text: "STORE", width: 0.5, color: Color.black, fontSize: geo.size.width * 0.08)
     }
   }
}

如果.foregroundColor不工作,我如何设置文本的填充颜色?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 03:15:30

这是因为您正在为文本设置笔触颜色。您需要做的就是为笔划设置一个负值:

代码语言:javascript
复制
.strokeWidth: fontSize * -0.05
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65027874

复制
相关文章

相似问题

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