问题
I want to allow bolding, italic ,underlines ,text-colors, etc in a UITextField.
Is there an external library that allows that? or am i suppose to write it myself?
Edit:
I want the user to be able to edit a portion of the text they way they want, let's say the first word would be bold, the second would be italic, and the third word would be normal
回答1:
Out of the box, you can only format a UITextField in its entirety.
There are some libraries that will help you display text using attributed strings but if you need editing capabilities you will have to pretty much write it all from the ground up using CoreText and UITextInput delegation.
From the research I've done, even the magnifying loupe and text selection controls will need to be recreated from scratch.
Have a look at the Omnigroup framework released on Github https://github.com/omnigroup and look for their Rich Text editor which will give you an idea of what's involved.
There's an alternative to this with the next iOS release and UIWebView but it cannot be discussed here as it is stil under NDA so you may want to head to the Apple forums if you need more info on it.
回答2:
A UITextField has its own properties that can be set, such as font, textColor, textAlignment, and text. Icodeblogwrote up a great overview on everything that can be done with a UITextField here (including examples): http://www.icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/
回答3:
So I haven't tried this, but what happens when you pass an NSAttributedString to UITextField? If this works then you could possibly add your own controls to do the editing of the attributed string. Possibly via an accessory view to the standard keyboard.
This also might help: https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
来源:https://stackoverflow.com/questions/6487290/iphone-uitextfield-with-rich-text