带有占位符的UITextField导致应用程序崩溃

带有占位符的UITextField导致应用程序崩溃

本文介绍了Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 11 Beta版本和iOS 13模拟器中,访问TextField _placeholderLabel.textColor标签键时发生崩溃.

In Xcode 11 Beta Version and iOS 13 Simulator getting a crash when access TextField _placeholderLabel.textColor label key.

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

The key used to apply placeholder text color.

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

推荐答案

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

You can do it by using runtime:

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

add the following code to the bottom of placeholder setting

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

在Xcode 11 beta2上,此代码有效,但我不知道GM版本或正式版本.

At Xcode 11 beta2 ,this code is work ,but I don't know about GM version or official version.

完整代码:

  • 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更新

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

    2019/09/25 Update

    The above implementation can solve the problem ,but it not be advocated.

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

    The apps that use the private api maybe broken in the future.

    请使用新的API:

    var attributedPlaceholder: NSAttributedString? { get set }
    

    讨论

    完整代码:

    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)
    
    

    这篇关于Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:30