Split text if its content exceeds numbers line limit in Swift - ios

Conditions:
UITextView that can contains X lines as maximum:disable scroll. (For example; max lines = 8)
A huge text content. (For example; 25 lines)
Language is English. (Word-wrap is compulsory)
How can I split text if its content exceeds numbers line limit?
I want to split text into array. Following the condition, the text should be split to 4 elements.
Update:
Actually what I want to achieve is like when I have a long text and I have the fix text container size (eg. UITextView) which cannot display all content, then there is a next button to reset container and display the left content. I thought that split string would help but it seems a wrong choice. I am trying what Matt's suggestion and hope the problem will be solved.

Your goal is not a good one. Instead of splitting the text into four arrays of text, it would be better to take advantage of Text Kit and let the layout manager split the text into four text containers (one NSLayoutManager, four NSTextContainers).
This could even be combined with your current use of a text view for display purposes: the four text containers could belong to four text views, and now you have four noneditable nonscrollable text views that automatically distribute the text between them, and you can just switch among those text views.
Here's an example with just two text views:
That's two UITextView objects governed by one NSLayoutManager, so that as the text becomes too long for the first text view it automatically flows into the second text view.
If you really wanted to, I suppose you could do what I just said and then use the layout manager to ask what it did, i.e. to read backwards from the line fragments to the glyphs to the character ranges and find out how it split the text into four text containers; but this seems silly when you consider that the problem is already solved before you even start to do that.

Related

NSTextView - using up/down arrows moves paragraphs not lines

I have an app that runs on iOS and uses UITextView to display rich text, and also runs on Mac and uses NSTextView to display rich text. These are separate apps, but with a lot of shared code. In both cases, the {NS,UI}Textview is programmatically created and placed on a canvas.
{NS,UI}TextView will wrap long lines of text as required. Long lines are called paragraphs and have NSParagraphStyles that can be applied across the whole paragraph (line spacing, head indent etc). Each paragraph is delineated by a "\n" character (or one of the Unicode equivalents). Paragraphs wrap across multiple lines, if required.
On iOS, when you have a long paragraph covering multiple lines... if you use the down/up arrow keys, the cursor moves up and down by lines (i.e. possibly within the same paragraph), which is as you expect.
On the Mac, with the same setup, the up/down arrow keys move the cursor up and down by paragraphs (i.e. multiple lines), which is very much not what you expect.
I have attached videos of the two cursor movements so you can see what happens.
I use the same code to create the {NS,UI}TextView, and I can't see why there is a difference.
Does anyone know what's going on here? I have not interfered with the arrow key handlers in any way (although I do implement textViewDidChangeSelection: in the delegate, and can confirm that the selection is changing per my comments above).
Any ideas would be most appreciated!
Thx.
iOS Cursor Down in a UITextView.
Mac Cursor Down in an NSTextView.
Found it! It was an errant calculation in the delegate function DoCommandBySelector.

Custom iOS keyboard - Custom font output

I am trying to create a custom keyboard using the app keyboard extension. I am happy with the layout but the output is depended on the UITextField's font.
Is there a way to force a different font (use special characters?) while using the keyboard ?
Thank you
It depends.
Text field (or any other view that draws text) uses 2 informations on how to show some text. One is the sequence of characters called String and the other one is how the string should be represented. The second one is then split it things like fonts, colors, line height, line breaking and wrapping...
So the keyboard alone is not enough to for instance present a certain part of word using different fonts. You need at least a bit of access to the item that represents the text. So if you have no access to your text field then the answer is; No, you can not fore a different font when using different keyboard.
If you do have the access then the answer should lie in NSAttributedString. It is a string you can assign to most items under attributedText. This class wraps your raw string and can add many properties to parts of text you want to change. That includes using a different font.
Another approach would be using HTML tags. Again you will need to process this using for instance NSAttributedString or display it with another element like web view.
I would try it with using NSAttributedString. Hook up to delegate and implement textField(: shouldChangeCharactersIn: replacementString:. The implementation itself may still not be easy though.

Creating a signup form using a tableview in xcode (like SnapChat)

I am looking to improve my app's signup and login form from standard text boxes to using table rows (or something similar).
I am pretty much inspired by the forms which SnapChat have created, which you can see below...
I'm trying to figure out how they accomplished this, and am thinking it must be a table view with 3 cells (Static?), and the text below is part of the footer of that selection of cells.
How is one able to then capture data into it directly?
Am I right in maybe assuming that each cell is 'custom' with a text field in each one (with placeholder text) with a no-borders style on the text field?
Or has this been achieved via some other way?
Keen to hear your thoughts. Thanks!
You absolutely could do that with a table view. But I don't think this one is. Notice the divider lines start at the far left rather than at 15 pixels in (with the text). The separator lines are also two pixels tall rather than one. So I'd say it's a safe bet that it's just a simple view with 1 pixel tall UIView's for lines.

UserInteractive Substring in text Label

i am generating a label in a view with label text retrieving from a JSON file.
now there are certain texts(values) retrieved from the JSON file that are user interactive texts (URL/some action) and some are not.
Now i am retrieving the data which are user interactive, setting them as text of a UILabel (which i created dynamically) and setting it in the view (PS. my view controller only has a 2 views inside,one that is default and another in which i am generating the label).
The problem that is arising is that the interactive strings(labels) are set in one line and the ones that are not interactive are set in the next line(as i am creating individial labels for them) even if its a single word.
I want to set the labels (with the texts - interactive/noninteractive) side by side untill there is no space.
Cant wrap my head around it because its a logical problem.
Any help is appreciated

Read database, put the text in two columns to fill them, and same the rest iPad

I want to build an app for iPad like a book, with two pages, with one column each.
After read some text from a database, I want to fulfill the two pages (with the right quantity of text), and save whats left to the next page. I will display some images between the text, and I need to make the text and image to fit in each page.
I'm thinking in use a UIWebView to hold the text.
My problem it's calculate the amount of text needed for each page and the text that left.
Can you help me doing that?
I will display some images between the text, and I need to make the text and image to fit in each page.
UIWebView is the wrong UI element to use if you just want to display text. You should know that loading a UIWebView takes long and eats up a lot of memory. This is a job for UITextView.
To calculate the text that will fit on a page, you will have to use the methods that UIKit adds to NSString. Here is the documentation. These help you to calculate the size a string will be when rendered with a certain font and certain line break mode.
CGSize renderedTextSize = [myString sizeWithFont:myFont forWidth:myTextView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
You could write a method that would use these methods calculate a substring of the text that will fit on your current "page" (UITextView). Then, for the next "page", start from where you left off on the last page and add text to that string until it fits perfectly in your text view (maybe you add words until it gets too big to fit, then take out the last word and return that substring).
Something like that should work.

Resources