【问题标题】:foregroundColor for Textfield in swiftswift中文本字段的foregroundColor
【发布时间】:2017-11-07 13:55:25
【问题描述】:

亲爱的,

我正在快速学习,我正在尝试为文本字段文本着色,现在的问题是颜色未应用于文本。请在下面找到代码:

     let textStyle:[String:Any] = [
        NSAttributedStringKey.strokeColor.rawValue: UIColor.black,
        NSAttributedStringKey.foregroundColor.rawValue: UIColor.white,
        NSAttributedStringKey.font.rawValue: UIFont (name: "Impact", size: 50)!,
        NSAttributedStringKey.strokeWidth.rawValue: 3.5
        ]

谢谢

【问题讨论】:

  • From apple "渲染时使用该属性指定文本的颜色。如果不指定该属性,则文本呈现黑色。"所以你在白色上设置白色没有? Foregroundcolor 似乎修改了文本颜色,我想这不是你想要做的?
  • 删除.rawValuetextStyle 也应该是 [NSAttributedStringKey:Any] 类型而不是 [String:Any]
  • 您是否意识到您可以简单地设置UITextField 的属性? myTextField.textColor = UIColor.black
  • 我尝试使用 .textColor 直接更改它及其工作,但当我添加描边颜色时却没有

标签: ios swift colors textfield swift4


【解决方案1】:

关键似乎是.strokeWidth...如果你想要描边和填充,描边宽度需要是一个值。

试试这个:

    let textStyle = [
        NSAttributedStringKey.strokeColor: UIColor.black,
        NSAttributedStringKey.foregroundColor: UIColor.white,
        NSAttributedStringKey.font: UIFont(name: "Impact", size: 50)!,
        NSAttributedStringKey.strokeWidth: -3.5
    ]

【讨论】:

    【解决方案2】:

    你能试试这个吗...

    myTextField.attributedText = NSAttributedString(string: "text",
                             attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多