将标签添加到UITextField [英] Add label to UITextField

查看:65
本文介绍了将标签添加到UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题 663830 ,Isaac询问如何在文本字段中添加按钮。有人可以显示代码来为.rightView属性添加标签吗?

In question 663830, Isaac asks about adding a button to a text field. Can someone show code to add a label to the .rightView property?

或者,是否有更好的方法在文本字段中包含永久文本?

Or, is there some better way to include 'permanent' text in a text field?

这是一个计算器,包括字段中的单位(mg,kg等),而不必在文本字段之外维护标签。类似于永久占位符文本?

This is for a calculator that would include units in the field (mg, kg, etc.) without having to maintain a label outside of the text field. Sort of like permanent placeholder text?

谢谢,
Chris

Thanks, Chris

推荐答案

快速而脏的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    textField.borderStyle = UITextBorderStyleLine;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    label.text = @"mg";
    label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = label;
    [self.view addSubview:textField];
    [label release];
    [textField release];
}

请注意,我正在从ViewController添加子视图,你可以这样做从视图来看。

Note that I'm adding the subview from the ViewController, you can do it from the view as well.

这篇关于将标签添加到UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆