如何在 MDCTextField 上设置插入符号颜色?

How do I set the caret cursor color on an MDCTextField?

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

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

mdcTextField.tintColor = .red

试试这个

override func viewDidLoad() {
    super.viewDidLoad()

     textfield.tintColor = .red

 }

我遇到了同样的问题,通过子类化 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

希望对您有所帮助!

感谢使用 MDC-iOS。

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

它包含在版本 38.1.0 中。

其他的都试过了。除了这个没有任何作用:

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'