I have a long string which I would like to present, splitted, in several text views using scroll view and PageControl.
Example:
Does anyone know of such a control?
If I'm making the textview with fixed size how can I calculate how much text can fit and splits the string to chunks?
You can go two ways in iOS7, each with their pros and cons.
You can go the TextKit way, define multiple text containers (each text area on each page), and use a single layout manager to draw your across the views. The pros of this is that it gives you a lot of control and versatility. The cons, as you have probably figured out, is it is difficult to implement for a small need such as this.
Another way is UIWebView pagination. What this does is create exactly what you need with three lines of code:
_webView.paginationMode = UIWebPaginationModeLeftToRight;
_webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
_webView.gapBetweenPages = 5;
The cons of this is that you have a webview, which is somewhat harder to manage (you load an HTML instead of text).
If you want to use an NSAttributedString have a look at my answer at iOS UITableView with dynamic text and images rendered together (NSAttributedString + images).
If you just want to use an NSString with one font size you can split your text, by finding out it's size: See Mark Ramotowski's answer here With what should I replace the deprecated sizeWithFont: method?
The docs:
https://developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/boundingRectWithSize:options:attributes:context:
I don't know, if someone wrote a drop in control without custom coding, as you asked for.
Ok,
Here is my solution :
https://github.com/rorlich/LongTextView
i'm guessing i will need to improve it in the future but it's a good start for now
Related
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.
I want to have a large number of text boxes which will be touch enabled and editable. Is creating NSArray of UItextField objects the best way for this? If Yes, How can I create? or Suggest other ways to achieve this.
It largely depends on what you are trying to do. An NSArray as a way to store all the text boxes you are using in you controller (instead of creating ivars for that purpose) is ok, but you could as well use a UITableView/UITableViewController for that.
Using a table view would give allow you to grow the number of your text boxes without any effort. On the other hand, if you can guarantee that your text boxes will never be more than those you can display on a single screen real estate, I don't think using a table view would give you big advantages. But, as I said, this largely depends on what you are trying to do.
If you decide to go for the array option, I would suggest using an NSDictionary instead, so that you can access each one of your views by name (or tag, if you associate a tag with each one).
Also keep in mind that you could use the getViewByTag: method on your container view to get a reference to any view that it contains based on the view tag you assigned. So, you could do:
//-- creating text box:
UITextField* textBox = ....;
textBox.tag = 1;
[self.view addSbview:textBox];
//-- accessing the text box:
UITextField* textBox = [self.view getViewByTag:1];
In this sense, a view already behaves as a container for you text boxes and gives you access to them.
EDIT:
Actually I'm trying to create a crossword grid
ok, so, if it's 2-dimensional, I would say that a table view is ruled out (it is not impossible to do, but I think there are easier ways).
as to your question, it all depends on how dynamic your crossword grid is: does it always have the same number of rows and columns? or can it be defined by the user? etc.
In the first case, I would go for an NSArray, or I would simply use tagging as shown above (that would also make memory management automatic).
Otherwise, you might inspect UICollectionView.
If your question is: which data structure is more appropriate to handle a crossword puzzle? then, have a look at this post. In any case, I would say: do not expect that you find a ready-made solution for that kind of problems...
A UITableView containing editable cells would be the best way to do this, if you're after what I think you're describing. There's lots of sample code on Apple's developer site detailing how best to use a table view to create a view showing a series of editable text inputs.
Better to use UI Table View instead of adding 'n' number of text fields.
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
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.
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!