【问题标题】:Putting a readonly title in a UITextField将只读标题放入 UITextField
【发布时间】:2009-10-08 08:12:35
【问题描述】:

如何将readonly 文本放入UITextField。例如,iPod touch“其他网络”设置中的“名称”和“安全”字段分别包含“名称”和“安全”这两个词,右侧有一个可编辑区域。 UITextField 有一个“占位符”属性,但我一输入它就消失了。

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    只需将这一行放在txtname 是UITextField 的对象处:

    txtName.userInteractionEnabled = NO;
    

    【讨论】:

    • 这确实使文本字段文本不可编辑,但最初的问题是关于在文本字段之前附加标签而无需创建第二个控件。 +1 来自我
    【解决方案2】:

    您在这些字段中看到的不是 UITextField 有标签,而是 UITableViewCell 上有两个控件 - 一个标签和一个没有边框的文本字段。

    【讨论】:

    • 我只需要两个文本字段,所以我希望避免实现 UITableView。
    • 实现一个 UITableView 来托管两个文本字段并没有什么错误或困难 - 并且控制标签的 UITableViewCell 是一个奖励。
    • 你其实不需要实现一个UITableView。只需添加一个 UILabel 和一个 UITextField(无边框)
    【解决方案3】:

    在你想要的 UITextField 框架内放置一个 UILabel,然后将 UITextFields“leftView”属性设置为 UILabel。确保将 UITextFields“leftViewMode”属性设置为 UITextFieldViewModeAlways

    代码应该看起来像这样..

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 200, 44)];
    textField.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, textField.frame.size.height)];
    label.font = [UIFont boldSystemFontOfSize:14.0f];
    label.text = @"Name:";
    textField.leftView = label;
    textField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:textField];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多