如何调整UITextField占位符文本以适应UITextField宽度?

时间:2016-08-08 06:52:24

标签: ios objective-c uitextfield

可以更改UITextField占位符文本颜色,如下所示:

[txtField setValue:[UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]
                    forKeyPath:@"_placeholderLabel.textColor"];

同样有什么方法可以调整UITextField占位符文本字体以适应UITextField宽度?

注意:我在表格视图的自定义单元格中有文本字段,并且在自定义单元格中也使用自动布局。

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,这是解决方案:

UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:@"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;

答案 1 :(得分:0)

以下是我在ViewDidLoad中使用的一种编码,用于设置所需大小的文本字段的占位符。 - - - - - - - - - - - - - - - - - -码 - - - - - - - ---------------------------

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
textfieldUsername.leftView = paddingView;
textfieldUsername.leftViewMode = UITextFieldViewModeAlways;

[[UITextField appearance] setTintColor:[UIColor whiteColor]];
[textfieldUsername setValue:[UIColor lightTextColor] forKeyPath:@"_placeholderLabel.textColor"];

(新泽西州)

答案 2 :(得分:0)

这是代码

 textField.placeholder = @"Pavankumar";
 [textField setValue:[UIColor greenColor]
                    forKeyPath:@"_placeholderLabel.textColor"];

见下面的代码,它改变了占位符的字体

textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"Pavankumar" attributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-light" size:12.0]}];

答案 3 :(得分:0)

您有占位符文本,并且您拥有uitextfield的框架。 从这两个中,您可以计算出适合的字体大小,并将其指定为属性占位符字符串。

如果计算的字体大小小于您的限制,则使用该字体大小另外使用限制