自定义 UITextField/UIButton
我想做的是复制 NSTokenField 就像邮件应用程序和消息应用程序中看到的 UITextField (输入联系人,它会提出建议)。
我的自动完成工作完美,当您输入 UITextField 时,会弹出一个 UITableView,显示它可以在数组中找到的任何匹配项,当您单击一个时,它会将其添加到 UITextField 中。到目前为止我真的很高兴。
我现在遇到的问题是让控件看起来像本机应用程序中的控件。毕竟,设计就是一切!
我的第一个问题是如何将阴影外观添加到 UITableView 中?看起来它沉入了 UITextField 后面。
其次,我知道我必须对 UITextField 进行子类化,以使其看起来像我想要的那样,但我不知道从哪里开始。一些指示或示例会很棒!
最后,我想我需要创建一个带有文本空间和蓝色渐变的自定义 UIButton,然后将其添加到 UITextField。与 UITextField 相同的问题,不太确定如何子类化 UIButton (它需要绘制和填充哪些方法)或如何将其添加到 UITextField 中,这样当您在 UITextField 中单击退格键时,按钮将突出显示,然后如果再次单击退格键则将其删除(这正是 NSTokenField 的工作原理)。
我添加了一张图片,以便您可以了解我在说什么: http://www.thermoglobalnuclearwar.com/stuff/mail.jpg
我已采取看了一下 Joe Hewitts Three20 项目,但我无法理解它。 我想非常简单地开始并了解正在发生的一切,而不是只是将他的代码拖到我的代码中而不知道发生了什么!
非常感谢任何帮助!
谢谢, 汤姆.
What I'm trying to do is replicate the NSTokenField like UITextField seen in the Mail app and Messages app (type a contact and it comes up with suggestions).
I've got the autocompleting working perfectly, when you type in a UITextField, a UITableView pops up showing any matches that it can find in an array, when you click one it adds it to the UITextField. I'm really happy with this so far.
The problem I've run into now is making the controls look like those in the native apps. Afterall, design is everything!
My first question is how can I add that shadow look to the UITableView? Looks like it's sunk down behind the UITextField.
Secondly, I know I'm going to have to subclass the UITextField to make it look the way I'd like it to, but I've got no idea where to start with that. Some pointers or a sample would be great!
Lastly, I think I need to create a custom UIButton with space for text and the blue gradient then add it to the UITextField. Same problem as with the UITextField, not really sure how to subclass the UIButton (what methods it needs to draw and stuff) or how to add it to the UITextField in such a way that when you click backspace on in the UITextField, the button will be highlighted, then deleted if backspace is clicked again (exactly how the NSTokenField works).
I've included an image just so you can see what I'm talking about:
http://www.thermoglobalnuclearwar.com/stuff/mail.jpg
I have taken a look at Joe Hewitts Three20 project but I couldn't make heads or tails of it.
I'd like to start very simply and understand everything that's going on rather than just dragging his code into mine and not having any idea what's going on!
Any help is greatly appreciated!
Thanks,
Tom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 Three20 库?它包含一个我认为可以实现您想要的功能的控件(TTPickerTextView)。
正如网站描述所述
源代码至少可以为您提供一些有关如何实现各种视觉效果的指导。
Have you considered using the Three20 library? It contains a control which I think does what you want (TTPickerTextView).
As the website description states
At a minimum the source code might give you some pointers on how to achieve the various visual effects.
好的,我已经在 UITextField 下设置了阴影,并且添加了“收件人:”标签。看起来很棒!
所以最后就是蓝色的NSToken之类的控件了。我开始认为最简单的事情就是子类化 UIView 并在其中绘制蓝色渐变和标签。这给我带来了更多问题:
我发现了这个: http://github.com/ leonho/iphone-libs/tree/master 它绘制了一个漂亮的圆形视图,我已经对其进行了调整以添加一些文本而不是数字,我不知道如何做的是绘制渐变固体色块。
之后,只需将圆形视图添加到 UITextField、移动光标并研究如何在光标到达视图时删除视图,但我会在需要时解决这个问题。
Okay, I've got the shadow working underneath the UITextField, and I've added the "To:" label to it. It looks great!
So the final thing is the blue NSToken like control. I've started to think the easiest thing is just to subclass a UIView and draw the blue gradient and label inside it. Which brings me to some more questions:
I found this: http://github.com/leonho/iphone-libs/tree/master which draws a nice rounded view and I've adapted it to add some text to it rather than a number, what I don't know how to do is draw a gradient instead of a solid block of colour.
After that there's just the matter of adding the rounded views to the UITextField, moving the cursor and working out how to delete the views when the cursor reaches them, but I'll tackle that when I need to.