【问题标题】:How do I apply UIAppearance Proxy properties to UILabel?如何将 UIAppearance 代理属性应用于 UILabel?
【发布时间】:2012-08-04 01:25:32
【问题描述】:

在尝试将 UIAppearance 代理样式应用于 UILabel 类代理时,我得到了不可靠的结果。例如,以下工作符合我的预期:

[[UILabel appearance] setFont:[UIFont fontWithName:SOME_FONT size:SOME_SIZE]];
[[UILabel appearance] setShadowColor:[UIColor blackColor]];

设置 textColor 不起作用,但是,这个:

[[UILabel appearance] setColor:[UIColor greenColor]];

确实有效。 一种。这有点不可靠,并且会导致任何对 setTextColor: 的特定于实例的调用都被忽略。

将 UIAppearance 样式应用于 UILabel 的正确方法是什么?

【问题讨论】:

    标签: ios uilabel uiappearance


    【解决方案1】:

    好的,事实证明您无法使用UIAppearance 代理设置任何 UILabel 属性的样式

    虽然UILabel 类符合UIAppearanceContainer 协议,但对UILabel.h 的检查表明它的所有属性都没有标记为UI_APPEARANCE_SELECTOR,这是使用UIAppearance 的先决条件。

    臭虫。

    【讨论】:

    • 这对我来说仍然令人难以置信。 UIButton 也完全无法使用 UIAppearance 设置样式。我在 2012 年 3 月提交了一份雷达。只能希望他们在 iOS 7 中解决这个问题。
    • 这太可怕了!太可怕了!
    • 我不相信这仍然是真的。至少,我能够在我的应用程序中使用 UIAppearance 代理设置 UILabels 样式。我遇到的问题是我不希望我的标签样式化的区域。例如在 UISegmentedControl 中。我尝试使用 appearanceWhenContainedIn: 来解决这个问题,但这很复杂,我最终取出外观代理并手动设置样式。
    • 是的,@Mojo66,它确实有点工作,看看我原来的问题 - 它不可靠,至少它不是。
    • 在iOS11仍然无法配置颜色,是这个原因还是有这个原因?我不敢相信苹果没有人力来解决这个问题......
    【解决方案2】:

    我已经继承了 UILabel

    @interface SmallLabel : UILabel
    
    @end
    
    @implementation SmallLabel
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
        }
        return self;
    }
    
    @end
    

    然后我使用外观当ContainedIn:

    UIFont *smallFont = [UIFont fontWithName:@"Arial" size:15];
    [[SmallLabel appearanceWhenContainedIn:[UIView class], nil] setFont:smallFont];
    

    这可以为我的应用程序中的所有 SmallLabels 使用所需的字体。我只需要在 XCode 身份检查器中将自定义类设置为 SmallLabel。它似乎不适用于以编程方式创建的标签,仅适用于 NIB 中的标签。

    经过进一步测试,此方法并不总是可靠地工作。

    【讨论】:

      【解决方案3】:

      在 Swift 中,您可以执行以下操作来自定义包含在 UITableViewHeaderFooterView 中的 UILabel 的外观属性:

      UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 18)
      UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white
      

      当你在中使用 UITableViewHeaderFooterView 时,这将应用于 textLabel 属性:

       public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerViewId)
          if headerView == nil {
              headerView = UITableViewHeaderFooterView(reuseIdentifier: headerViewId)
          }
          headerView?.textLabel?.text = "My Header".uppercased()
          return headerView
      }
      

      这在使用 UITableViewStyle.grouped 时确实很有帮助,因为即使您在 viewForHeaderInSection 中自定义 UITableViewHeaderFooterView.textLabel,这些节标题视图似乎也被默认样式覆盖

      【讨论】:

        【解决方案4】:

        以下代码适用于我使用 swift 2.2 和 iOS 9.0

            let textFieldInsideSearchBar = self.searchBar?.valueForKey("searchField") as? UITextField
            textFieldInsideSearchBar?.textColor = BCGConstants.Colors.darkBluishPurpleColor()
        
            let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
            textFieldInsideSearchBarLabel?.textColor = UIColor(red: 220/255, green: 209/255, blue: 231/255, alpha: 1.0)`enter code here`
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-14
          • 1970-01-01
          • 1970-01-01
          • 2019-08-05
          • 1970-01-01
          • 1970-01-01
          • 2013-03-12
          相关资源
          最近更新 更多