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

Ios 如何调整UITextField占位符文本的大小以适应UITextField宽度?,ios,objective-c,uitextfield,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宽度 注意:我在表格视

可以按如下方式更改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宽度


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

以下是我在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"];
(Nj)

这是代码

 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]}];

您有占位符文本和uitextfield的框架。 根据这两个参数,您可以计算适合的字体大小,并将其指定为属性占位符字符串


如果计算的字体大小小于您的限制,则以其他方式使用该字体大小使用限制

我遇到了相同的问题,以下是解决方案:

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

查看此项检查此项:Thnak用于链接,但是,我尝试了它,但似乎不起作用。我认为您误解了这个问题,我不希望文本字段占位符文本的缩放字体color@Pavankumarnot工作我想缩放文本字段占位符文本的字体,我有固定宽度的文本字段截断占位符text@Garry@Ronak Chaniyara解决了这个问题吗?我也有同样的问题。请告诉我你是怎么解决的!!谢谢