gpt4 book ai didi

ios - UITextField attributedPlaceholder 无效

转载 作者:技术小花猫 更新时间:2023-10-29 10:13:43 26 4
gpt4 key购买 nike

我正在尝试将我的文本字段中的占位符设为斜体,并且由于我的应用程序针对 iOS 6.0 或更高版本,因此决定使用 attributedPlaceholder 属性而不是滚动更多自定义内容。代码如下:

NSString *plString = @"optional";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString
attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15]}];
for (UITextField *t in myTextfields){
t.placeholder = plString;
t.attributedPlaceholder = placeholder;
}

但占位符的样式仍然不是斜体,而是与常规文本相同,只是颜色较暗。我缺少什么才能使 NSAttributedString 正常工作?

最佳答案

正如 warren 所指出的,样式目前无法按照您尝试的方式完成。一个好的解决方法是按照您希望占位符的外观设置文本字段的字体属性,然后在用户开始输入时更改文本字段的字体。看起来占位符和文本是不同的字体。

您可以通过创建文本字段的委托(delegate)并像这样使用 shouldChangeCharactersinRange 来做到这一点:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// If there is text in the text field
if (textField.text.length + (string.length - range.length) > 0) {
// Set textfield font
textField.font = [UIFont fontWithName:@"Font" size:14];
} else {
// Set textfield placeholder font (or so it appears)
textField.font = [UIFont fontWithName:@"PlaceholderFont" size:14];
}

return YES;
}

关于ios - UITextField attributedPlaceholder 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15031678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com