iOS, fast way to generate clickable, multicoloured text - ios

I need to generate a text container that contains something like this:
This is some random text
where a few of the words are
coloured and clickable
The clickable words should have different actions bound to them and should be in a certain colour. The container will have a fixed width and I need to know the resulting height of the container given a certain text.
What I've tried: Tried making each word a separate UILabel, added actions where actions were needed, calculated line breaks myself. The problem with this approach was that it was too slow, especially UILabel sizeThatFits. I need to generate a lot of text for a scrolling UITableView and this approach killed the scrolling performance.
What I also tried: UIWebView. For a few different reasons, it's just not an option.
What I would prefer: A solution that does not require third party code. This is optional, though if they are open source. iOS 7-only solutions are acceptable.
Lastly, what needs to be fast is the generation of the text and the measuring of its height. Determining where to click is allowed to take some time.

See that https://github.com/mattt/TTTAttributedLabel
..but i not understood about height

If iOS only is an option, watch the WWDC 2013 session 210 "Introducing Text Kit". They show things that are at least very similar to what you are asking for.

Related

Laying out UITableView contents

The title itself might a bit misleading and I believe the problem is best explained with an example.
Take Facebook's feed, for instance. Every item consists of the following:
Header (user's name, etc), static height
Main content (text, attachment), dynamic height
Footer (number of likes and a couple of buttons), static height
At a minimum, the main content has one of the following:
Title, optional
Textual preview, optional
Attachment (of variable height), usually an image, optional
So, a few possible combinations of parts:
Title
Title, text
Title, text, attachment
Text
Text, attachment
etc
I've come up with two approaches:
1. Sections for each post, cells for parts
Note: the UIKit-provided header and footer wouldn't do, as I need specific spacing between each item. Therefore, I'd use these to get the spacing needed.
Also, I'm using RxSwift. This approach breaks the simplicity of data binding to the table view, although there's always a more complex alternative.
2. Multiple cell types
Not sure. Separate cell type for each possible combination of the above? Seems like an overkill.
3. Single cell type
The easiest approach. But, considering all the optional parts, there's quite a few constraints to switch and tweak (depending on the attachment height). UIStackView is not an option evidently, since the attachment must take the whole screen width, whilst the other parts use custom padding on each axis.
I'm looking for an outside perspective here, mainly from those who happen to have solved a similar problem in the past. An example of code would also be lovely (especially if it's scalable).
Target: iOS 13+.
Will AutoLayout perform alright in each of these?
What approach would you use?

Presenting a Text Stream in iOS

i need to present a running text stream in my apps, i got the running text from my server and i can see it using NSLog that the stream is running. The text is change and have a new line every second.
Since i'm newbie in iOS i already made a research and there are couples that i still confuse :
1.Between UITableView and UIScrollView, which one you recommended regarding the performance?
2.What is the right library to use?is it llike NSStream or i just present it to my UITable?
Will appreciate if you can have a snippet code or tutorial for this.
Thanks...
I don't have time to write the code, as it's not simple.
You will want to create a UITextView and append the string to the text view as you recieve new content.
UITextView is a subclass of UIScrollView, so you can use all UIScrollView operations on it, including scrolling down to the bottom as new content is added. This is the best way to have good performance, as UIScrollView and UITextView are both tightly integrated into the graphics hardware on the device.
If the text is really big (hundreds of megabytes) then you should also delete old text from the top of the text view as you add new text to the end.
Just use the code you have now, but instead of NSLog(), append it to the UITextView.
IMHO, UITableView would be ideal choice, since it's well optimized to reuse its cells. If you use scroll view, 3600 lines (that's 1 hour log) will probably end up with 3600 text views (or labels), while for table view, it would always be the number of rows visible on the screen.
For data model, I think you can just use a big array, which is continuously fetched with data from the server, possibly truncated when it gets too big (I mean deleting the very old lines).
Remember the rule of optimization: get it working first before optimizing it.

How can I achieve this layout in iOS?

I'm trying to achieve a layout similar to this:
I'm puzzled as to how this can be achieved. The new features in iOS 6, container cells and autolayout, perhaps should be of help, but the application I saw this in is quite old and they implemented that without those new features.
What I want to achieve is a label, a textfield, and then another label which may have text long enough that it has to wrap to the following line, like in the image I attached.
A possible way to achieve this is to put a label with two lines, and put for example the underscore character repeated and then measure somehow where the underscores start and end and overlay the textfield in that area. But this is difficult and seems quite cumbersome.
How can I achieve that?
I actually think your underscore idea isn't exactly a bad one and might not be as hard as you think.
There are probably cleaner (though maybe slower) ways to do this, but the easiest that comes to mind is setting a delimiter in your string that you can use to break the string into components like this:
Why ^________^ you late to work this morning?
From here, [[myString componentsSeparatedByString:#"^"] objectAtIndex:1]; will give you your underscores.
Next step is getting the size, like this [underscoreString sizeWithFont:[UIFont fontWithName:#"<your font>" size:<your size>]];
Now you have the size of your underscore string which you can use to overlay your UITextField. Be sure to consider the width of your delimiters in your underscoreString, either append them or adjust the width of the UITextField accordingly. Bear in mind, there are still other things to consider here (what happens if the underscore string word wraps for example), but hopefully this answers your immediate question.
This will not be a best solution.
But I think this can be easily achieved with the help of autolayout.
First put a label on the custom cell on right of that put a textField and on right of that a label again.
Label --> TextField --> Label
Here the first label should be filled with the words prior to textField (How) on the other label you need to fill the remaining portion.
And set the relative position of textfield with first and last label. So that if first label content is much more bigger it'll move to right side

iOS to display a headline and a paragraph

I want to compose a view in an ios app that includes a headline and a paragraph. It will look a lot like an HTML page with h1 tag and p tag, how do I make them look like that in iOS? What controls should I use? I would prefer the answer to work with UI Builder instead of dynamical objects in code.
What I tried is using labels, and it didn't work well.
The long-standing approach for doing this is with multiple labels (UILabel), each in a different style/font/color. This is not difficult, but with multiline labels iOS will try to center the text vertically for you, so you have to lay out the height of the labels in code, using sizeWithFont:forWidth:lineBreakMode:, and then you have to position the labels correctly in code.
I must stress that this is not hard to do. It is tedious but it works.
However, in iOS 6, the problem is completely solved: you can just use NSAttributedString. This lets you create one string consisting of multiple paragraphs in different styles/fonts/colors, which is a way doing for real the very thing we were trying to simulate earlier using multiple UILabel objects. You get to dictate margins, spacing between paragraphs, etc. - the whole works. It's fantastic.
I suggest you watch the WWDC 2012 videos concerning attributed strings in iOS 6. Do not, however, believe their repeated mantra that UILabel is your locus of power for displaying attributed strings. I have found, for example, in rewriting my app, that multiparagraph attributed strings do not always play nicely with UILabel. So I have found it easiest to lay out the attributed string by drawing it directly in the drawRect: of a custom view, using the new NSAttributedString drawWithRect:options:context: method.
Why didn't labels work? I would recommend subclassing UIView to hold two labels (heading and paragraph) and style them appropriately. You'll need to specify what your issues with the labels were and what exactly you're trying to accomplish if you need further advice.

Is there an alternate to UIWebView for dynamically displaying formatted text?

Working on an app that needs to take a large amount of text, paginate it based on user selected font size, etc., and display it with styles. Getting UIWebView to paginate a document has proven extremely troublesome. I have seen a wrapper for Core Text that apparently, takes care of layout, but my understanding is that core text cannot be selected.
Anyone know how they accomplished this? I had assumed it was UIWebView.
Thanks in advance.
Prior to 3.2 displaying formatted text other than in a webview was nigh inpossible. Since then though we have NSAttributedString and corresponding UiKit elements which can display some font variations, colouring, formatting within one string. Pagination and other behaviour is outside of its scope though, and you would have to implement it yourself via a pageable scrollview as one possibility. Selection will also prove difficult, as you would have to implement it yourself again by ways of UiView hit detection on your UILabels and by implementing touchesBegan:, touchesMoved: and touchesEnded: and drawing the selection yourself.
Take a look at UITextView with Syntax Highlighting
Good luck!

Resources