首页 > 解决方案 > iOS 13 - 带有占位符的 UITextField 让应用程序崩溃

问题描述

在 iOS 13 中,我在访问UITextField_placeholderLabel.textColor 标签键时遇到了崩溃。

用于应用占位符文本颜色的键。

[textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

“NSGenericException” - 原因:“禁止访问 UITextField 的 _placeholderLabel ivar。这是一个应用程序错误”

标签: uitextfieldios13placeholder

解决方案


您可以使用运行时来做到这一点:

将以下代码添加到占位符设置的底部

Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];

在 Xcode 11 beta2 中,这个代码是可以工作的,但我不知道是 GM 版本还是官方版本。

完整代码:

  • Objective-C 版本

#import "ViewController.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"UITextField Demo";

    UITextField *textField = [UITextField new];
    textField.frame = CGRectMake(0, 100, 300, 50);
    textField.placeholder = @"UITextField Demo";
    [self.view addSubview:textField];

    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(textField, ivar);

    placeholderLabel.textColor = [UIColor whiteColor];
}

@end
  • 斯威夫特版本:
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        let textField = UITextField()
        textField.frame = CGRect(x: 0, y: 100, width: 300, height: 50)
        textField.placeholder = "UITextField Demo"
        view.addSubview(textField)

        let iVar = class_getInstanceVariable(UITextField.self, "_placeholderLabel")!
        let placeholderLabel = object_getIvar(textField, iVar) as! UILabel
        placeholderLabel.textColor = .red
    }
}

2019/09/25 更新

上述实现可以解决问题,但不提倡。

使用私有 api 的应用程序将来可能会损坏。

请使用新的 api:

var attributedPlaceholder: NSAttributedString? { get set }

讨论

此属性默认为 nil。如果设置,则使用系统定义的颜色和属性字符串的剩余样式信息(文本颜色除外)绘制占位符字符串。为该属性分配一个新值也会用相同的字符串数据替换占位符属性的值,尽管没有任何格式信息。为该属性分配新值不会影响文本字段的任何其他与样式相关的属性。

完整代码:

let textField = UITextField()
textField.frame = CGRect(x: 0, y: 100, width: 300, height: 50)
let placeholderString = NSAttributedString.init(string: "UITextField Demo", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
textField.attributedPlaceholder = placeholderString
view.addSubview(textField)


推荐阅读