uitextfield - iOS 13 - 带有占位符的 UITextField 让应用程序崩溃
问题描述
在 iOS 13 中,我在访问UITextField
_placeholderLabel.textColor 标签键时遇到了崩溃。
用于应用占位符文本颜色的键。
[textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
“NSGenericException” - 原因:“禁止访问 UITextField 的 _placeholderLabel ivar。这是一个应用程序错误”
解决方案
您可以使用运行时来做到这一点:
将以下代码添加到占位符设置的底部
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)
推荐阅读
- c# - C#方法将一行刮入一个数组 - 返回时不传递数组
- ruby-on-rails - 电报 webhook 路由未初始化
- python - 如何计算特定数据集的相关性
- r - 在 R 中使用 Matrxmodels 包时出错:CHOLMOD 分解不成功
- crystal-reports - Crystal Reports 子报表性能
- aem - AEM:如何扩展虚 URL?
- sql-server - 如何有效地支持 LIKE %Query%
- php - 与 PayPal 连接:获取状态 400(错误请求)
- office-js - 插件因检测缺失功能而被拒绝
- c# - ASP.NET Core 3.1 中 QueryString 字符串参数的自定义模型绑定器?