【问题标题】:How to Resize UITextField Placeholder Text to Fit UITextField width?如何调整 UITextField 占位符文本以适合 UITextField 宽度?
【发布时间】:2016-08-08 06:52:24
【问题描述】:

可以改变 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 宽度?

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

【问题讨论】:

标签: ios objective-c uitextfield


【解决方案1】:

我遇到了同样的问题,解决方法如下:

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

【讨论】:

    【解决方案2】:

    这是我在 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)

    【讨论】:

    • 不工作,我想缩放文本字段占位符文本的字体,并且我有固定宽度的文本字段截断占位符文本@Garry
    • @Ronak Chaniyara 是否设法解决了这个问题?我有完全相同的问题。请告诉我你是怎么解决的!!谢谢
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      这里是代码

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

      【讨论】:

      • 我认为你误解了这个问题,我想要文本字段占位符文本的缩放字体而不是 color@Pavankumar
      猜你喜欢
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2014-02-17
      相关资源
      最近更新 更多