How do you have two fonts in one UITextView (Xcode)? - ios

I'm trying to make two font styes in one UITextView, how do I do this?

From the class reference:
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.

You cannot have two on the same page because it is not supported. Just use a webview and an HTML file

You can know use this by using the attributedText property of UITextView. This is available under iOS 6

UITextView supports just a single font, but there's a different topic on something similar:
Can I use multiple font style in UITextView?
If the text you want to draw is simple, I'd suggest subclassing UITextView or UIView, overwriting the drawRect function and work with some extra variables of your own. This only works if you have a very predictable system to the fonts though.
Another option is using multiple labels, which would probably need an ever more predictable setup.

Related

iOS how to insert UITextField inline within UITextView

Is there a way to insert text inputs inline inside a block of text? I need to allow users to input some data inline within text. I thought that I would be able to use NSMutableString with attachments, but it seems to support only UIImage as attachments.
This is an example of what I'm trying to achieve:
Thanks!
You should take a closer look to CoreText which gives you a very fine grained access to text layout. But anyway, this is a difficult task. You may create a HTML-Page with input fields and display it in a WKWebView, which should be much easier to implement.
If the text is static, create a custom UIView class, and position the UILabel and UITextView controls. This is the only possible approach.
If the text is dynamic, you can make use of Auto-layout concept along with the above approach.

Use globally defined fonts in xib

What is the best way to enforce a standard set of fonts and sizes for an iOS-app?
Our app consists of about 150 xib-files, which of many have UILabels and UITextViews. The problem is that the font is set manually for each element in each of these xib-files, which leads to small inconsistencies here and there. I want them set globally one time, like small, medium and large, and then use those three defined fonts in the xib-files. Is it possible?
You can try user defined runtime attributes, which enable you set font name or font size, or layer border color etc for UIControl in Xcode.
You could use UIAppearance but, I believe, that this is only reliable in iOS 7+.
Otherwise, you could just subclass UILabel/UITextView, set the font in the init and then use those classes in your XIBs.
Your final option would be to use Pixate Freestyle and CSS.
Why you don't try FontReplacer like https://www.cocoacontrols.com/controls/fontreplacer
The source was removed on github but you can find in somewhere.
It does not immediately apply for xib files but working good at runtime.

UITextView with emoticons editing

I am developing xmpp-client app. One of the features is sending smiles and user should have ability to edit its like usuall text. Emoticons editing in Viber App is best example of what i want to implement.
I already tried three ways to solve problem:
I create emoticon like usual UIImageView and place it as a subview on UITextView using current caret rect. I use 5 whitespaces as a text placeholder in text view. There are two problems: with placing emoticons on new line when inserting text in the middle(printing whitespace not make caret move to new line); when user placing caret using magnify glass, he can move caret through emoticon(through 5 whitespaces), as delegate method not called during this process.
I have tried EGOTextView. There are problems with caret position and resizing when new line should be added. And there are some rendering artifacts when using it one line size.
I also have tried using UIWebView. But there were great problems with resizing based on text size and other artifacts with speed of response when becoming first responder.
May be some one could give me advice of really working solution?
I'm not really into rects and ranges mechanism of UITextView/UITextInput, but I try to give you an (untested) advice that maybe could achieve the expected result.
Built-in iOS emojis are simple characters, so we can follow the same path by building a custom font (or extend an existing one).
We have two options:
If you want to target iOS 6.0 (that has native support for NSAttributedString in UIKit classes), you could try to build a custom font containing all the emoticons you need, and use it inside your NSAttributedString (attributed string can mix different fonts, font sizes, styles and so on).
You could do something similar with iOS 5, but since you can't use NSAttributedString inside UITextView (so you are limited to just one font for the entire text) you should use a font that combines together the actual characters and the custom emoticons: so you should extend the font you want to use for typing, by adding all the emoticons to it. I don't know if this could have licenses implications, anyway.
Otherwise, you could always go much more low-level, implementing your own custom textView using CoreText, but I think it would be a hard work.

Changing the font of every UILabel in a large app

Here's an interesting problem for you:
We are in the process of re-skinning our entire app, which consists of over 100,000 lines of code and almost 100 XIB files. The new design requires (almost) every label in the app to use a new custom font, whereas the old app uses the default font of Helvetica Neue.
What is the most efficient way of changing as many of the UILabels as possible? We don't want to have to manually change thousands of labels both in code and in XIBs (which is especially difficult because we are using a non-system font).
We've discussed swizzling UILabel's creation methods to default to using the new custom font, which would still allow for the few labels that would remain in Helvetica Neue to be customized after creation.
Thoughts?
Take a look at NUI https://github.com/tombenner/nui.
You can customize a lot controls with it using a CSS-like stylesheet and implement it via subclasses or categories.
You should probably subclass UILabel and override either an initmethod of your choice, or awakeFromNib, and set the custom font there. Then you can go through the xib, either manually, or open it up in a text-editor and add a line like
<string key="X.CustomClassName">YourCustomLabelClass</string>
To set the specified label to your custom label class.
Swizzling is a good option . If you want to do it other way and have problems with custom fonts, I usually make a trivial subclass of UILabel that changes it's font in awakeFromNib for each custom font so I can lay them out in the interface builder.
But... This comes to my mind:
[[UILabel appearance] setFont:xxx];
I am not sure how you would deal with keeping the size, though. Perhaps swizzle is the best choice

How can I put icons inside a UITextField?

I want to create a custom input view, some of whose buttons insert "icons" into the selected UITextField, like the "RSS" or "Reader" icons which sometimes appear on the right hand side of the URL input field in Mobile Safari. I want the icons to be intermingled with the text and for all intents and purposes (like selection, cut and paste and deletion) behave like normal character glyphs.
Is there a name for these icons? Is there an API for creating them or do I have to build this entirely from scratch?
The leftView and rightView properties (instances of UIView) of the UITextField class are exactly for this purpose, and you can control their behaviour using the leftViewMode and rightViewMode properties. I suggest you to read the UITextField class reference for further details.
According to this question, what I want is an NSTokenField and there's not one available in stock iOS. However, there are some implementations on GitHub.

Resources