【问题标题】:How do I set the caret cursor color on an MDCTextField?如何在 MDCTextField 上设置插入符号光标颜色?
【发布时间】:2017-10-17 14:02:21
【问题描述】:

我正在使用MDCTextInputControllerFilled 并设置activeColor 属性会更改下划线和浮动占位符。但是,我找不到设置闪烁光标颜色的方法,默认情况下它是蓝色的。 有没有办法改变颜色?

【问题讨论】:

    标签: ios material-components


    【解决方案1】:

    我遇到了同样的问题,并通过子类化 MDCTextField 并覆盖 layoutSubviews 以仅在布局视图后更改 tintColor 来解决它。这对我有用。

    例如:

    AppaceaTextField.h

    #import "MaterialTextFields.h"
    @interface AppaceaTextField : MDCTextField
    @end
    

    AppaceaTextField.m

    #import "AppaceaTextField.h"
    @implementation AppaceaTextField
    - (void) layoutSubviews{
        [super layoutSubviews];
        self.tintColor = [UIColor redColor];
    }
    @end
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      感谢您使用 MDC-iOS。

      光标颜色刚刚被 added 作为 MDCTextField (.cursorColor) 上的参数。

      它包含在 38.1.0 版本中。

      【讨论】:

        【解决方案3】:

        由于MDCTextFieldUITextField 的子类,您应该更改tintColor 属性以更改光标的颜色:

        mdcTextField.tintColor = .red
        

        【讨论】:

        • 谢谢,但光标颜色保持不变。 MDCTextField 一定有什么特别之处。 mdcTextField.tintColor = .red 无效。
        【解决方案4】:

        试试这个

        override func viewDidLoad() {
            super.viewDidLoad()
        
             textfield.tintColor = .red
        
         }
        

        【讨论】:

        • 是的,但是当我这样做时没有任何反应。 MDCTextField 中必须有一些覆盖。
        【解决方案5】:

        尝试了其他所有方法。除了这个没有任何作用:

        let colorScheme = MDCSemanticColorScheme()
        colorScheme.primaryColor = .systemBlue // <-- This works in my case
        colorScheme.errorColor = .systemRed
        
        let container = MDCContainerScheme()
        container.colorScheme = colorScheme
        
        let textField = MDCTextField()
        let controller = MDCTextInputControllerUnderline(textInput: textField)
        controller.applyTheme(withScheme: scheme)
        

        对于上下文:

        pod 'MaterialComponents/TextFields', '~> 104.0.1'
        pod 'MaterialComponents/TextFields+Theming', '~> 104.0.1'
        

        【讨论】:

          猜你喜欢
          • 2019-09-17
          • 1970-01-01
          • 2022-09-27
          • 1970-01-01
          • 1970-01-01
          • 2012-07-21
          • 2018-10-28
          • 2010-10-20
          • 2020-12-01
          相关资源
          最近更新 更多