Pra*_*abu 16 placeholder uitextfield ios13 xcode11
在Xcode 11 Beta版本和iOS 13模拟器中,访问TextField _placeholderLabel.textColor标签键时会崩溃。
用于应用占位符文本颜色的键。
[textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
“ NSGenericException”-原因:“禁止访问UITextField的_placeholderLabel ivar。这是一个应用程序错误”
iDe*_*Orz 18
您可以使用运行时来做到这一点:
将以下代码添加到占位符设置的底部
Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)
在Xcode 11 beta2上,此代码有效,但是我不知道GM版本或正式版本。
完整的代码:
Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
以上实现可以解决问题,但不建议这样做。
使用私有API的应用程序将来可能会损坏。
请使用新的api:
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
}
}
Run Code Online (Sandbox Code Playgroud)
讨论区
默认情况下,此属性为nil。如果设置,则占位符字符串将使用系统定义的颜色以及属性字符串的其余样式信息(文本颜色除外)绘制。为该属性分配新值也将使用相同的字符串数据替换占位符属性的值,尽管没有任何格式信息。为该属性分配新值不会影响文本字段的任何其他与样式相关的属性。
完整的代码:
var attributedPlaceholder: NSAttributedString? { get set }
Run Code Online (Sandbox Code Playgroud)
aft*_*han 12
是的,这个问题与 Xcode 版本有关。我有 Xcode 11.2.1,我面临同样的问题。
_placeholderLabel.textColor
Run Code Online (Sandbox Code Playgroud)
如果您将故事板中的它用作运行时变量并遇到问题,则只需删除“_”
删除“_”运行时 Var 后开始工作
moh*_*sin 10
删除下划线“_”并尝试此操作。
[textfield setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
它应该适用于所有版本!
我是这样做的:
对象
NSMutableAttributedString *placeholderAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:searchField.attributedPlaceholder];
[placeholderAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [placeholderAttributedString length])];
searchField.attributedPlaceholder = placeholderAttributedString;
Run Code Online (Sandbox Code Playgroud)
迅捷5
var placeholderAttributedString = NSMutableAttributedString(attributedString: searchField.attributedPlaceholder)
placeholderAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: placeholderAttributedString.length))
searchField.attributedPlaceholder = placeholderAttributedString
Run Code Online (Sandbox Code Playgroud)
有点长,但是我暂时没有更好的选择。
| 归档时间: |
|
| 查看次数: |
10505 次 |
| 最近记录: |