iOS - UILabel 更改禁用标签的外观

发布于 2024-12-21 09:34:28 字数 182 浏览 0 评论 0原文

进一步回答我昨天的问题,我正在使用带有 UILabels 的视图,输出到外部监视器。

标签上可以设置文本,除非它们被禁用。但是(显然)禁用的标签的文本显示为灰色。有什么办法可以覆盖这种行为吗?

我唯一能想到做的另一件事是子类化 UILabel 并有一个变量将其设置为可编辑,但这会影响序列化吗?

谢谢!

Further to my question yesterday, I am using a view with UILabels, output to an external monitor.

The labels can have text set on them, unless they are disabled. But (obviously) a disabled label has it's text greyed out. Is there anyway to override this behaviour?

The only other thing I can think of doing is to subclass UILabel and have a variable setting it as editable, but would that affect serialization?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橙味迷妹 2024-12-28 09:34:28

如果您想阻止用户与标签交互,可以设置一个名为 userInteractionEnabled 的属性。

If you want to prevent user interaction with the label, there is a property named userInteractionEnabled that you can set.

天气好吗我好吗 2024-12-28 09:34:28

没关系,行为似乎无法改变。我只是对 UILabel 进行了子类化并添加了一个变量来检查它是否可编辑。干杯,伙计们。

Nevermind, it seems that behaviour cannot be changed. I simply subclassed the UILabel and added a variable to check if it was editable. Cheers guys.

呆头 2024-12-28 09:34:28

是的,可以设置颜色和 alpha 属性。

myLabel.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.5]
//                          set 'alpha' to something between 0-1 ------^^^

Yes It is possible set the color and alpha attribute.

myLabel.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.5]
//                          set 'alpha' to something between 0-1 ------^^^
慈悲佛祖 2024-12-28 09:34:28

如果您需要使用 disabledTextColor 的标签,类似于 highlightedTextColor,您必须子类化 UILabel 并使自定义文本绘制

Swift

final class Label: UILabel {
  var disabledTextColor: UIColor? {
    didSet { setNeedsDisplay() }
  }
  
  override func drawText(in rect: CGRect) {
    guard
      let color = disabledTextColor,
      
      !isEnabled,
      
      let font = font,
      let text = text
    else {
      super.drawText(in: rect)
      return
    }
    
    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = textAlignment
    
    NSAttributedString(
      string: text,
      attributes: [
        .font: font,
        .paragraphStyle: paragraph.copy() as! NSParagraphStyle,
        .foregroundColor: color
      ]
    )
    .draw(in: rect)
  }
}

If you need to have label with disabledTextColor, similar to highlightedTextColor, you have to subclass UILabel and make custom text drawing

Swift

final class Label: UILabel {
  var disabledTextColor: UIColor? {
    didSet { setNeedsDisplay() }
  }
  
  override func drawText(in rect: CGRect) {
    guard
      let color = disabledTextColor,
      
      !isEnabled,
      
      let font = font,
      let text = text
    else {
      super.drawText(in: rect)
      return
    }
    
    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = textAlignment
    
    NSAttributedString(
      string: text,
      attributes: [
        .font: font,
        .paragraphStyle: paragraph.copy() as! NSParagraphStyle,
        .foregroundColor: color
      ]
    )
    .draw(in: rect)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文