I am trying to display an article in a ViewController.
The output that I receive from the API divides the article into paragraphs, and each paragraph is a node in the JSON. For example,
{
"para" : 1,
"content" : "some long paragraph"
},
{
"para" : 1,
"content" : "some long paragraph"
}
I want to display this content as one long article, but I want to let each paragraph to be viewed in one textView instead of combining all the paragraphs and view in one textView.
I am on Xcode 8, Swift 3. I use Cocoapods, so libraries are welcome.
If you want to use Text view , then you don't need to create multiple textview. Just use a single textview.
Use a for loop on your response object and append all the paragraph's content in a single string using /n after each paragraph. And then simply set that string value into textview. That's it. I hope it will work for you better..
Related
I've got a UIButton, it's a simple segue to another page.
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line.
However, it is all left justified. When I select "Align Centre" (using the buttons just under the "Title", the word wrap no longer works and simply runs .... so you can't see it all. (e.g. "next pa" instead of "next page")
Am I missing something here? It seems like such a trivial thing to do! There's an old answer here can't get word wrap to work on UIButton but it's both old and uses code - surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line. However, it is all left justified.
Once you've decided to use an attributed string, you must do everything with the attributed string. So give your attributed string a paragraph style that centers the text.
let para = NSMutableParagraphStyle()
para.alignment = .center
para.lineBreakMode = .byWordWrapping
let s = NSAttributedString(
string: "Hello World", attributes: [.paragraphStyle : para])
self.button.setAttributedTitle(s, for: .normal)
You will also need to set the button's title label to allow multiple lines.
self.button.titleLabel?.numberOfLines = 0
Result:
surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
Not to center it, no; you can set the centering in the storyboard. So you could eliminate the first batch of code and configure it in the storyboard. But you must use code to turn the title label into a multiline label:
self.button.titleLabel?.numberOfLines = 0
There is no way to do that in the storyboard, because you have no access to the title label there.
I've just been playing round with this and I've found it works if you set it to 'character wrap' rather than 'word wrap' once you've selected centre alignment.
If anyone has a better solution please add it, as I guess this might have issues if you slightly change the width etc when using auto layout for different screen sizes etc if you want it to adapt its width so this might not be the best solution but it does work
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.
I'm trying to accomplish an autocomplete of my own for matching array items while typing. So, I already have the logic done for matching what's being typed with what's in the array, but my question is related to displaying the proposed correction in the UITextView like this screenshot
I was trying by splitting the text in the textview and replacing the last word into attributed strings, but that makes the proposed correction part of the actual contents. Any ideas of how this is accomplished in this app?
What you currently do is good , when you set the text of the textView to attributed string make bool flag say it's name is textEdited = true with a string part that the user types say it's name userStr , when textView change method is triggered check that bool and according to it make the search if it's true proceed search with userStr if it's not proceed search with whole textView text , don't forget to make textEdited= false after every zero suggested result
Edit: regarding the cursor put a label under the textfield with the same font as the textView and make it's background darkGray , also make background of the textview transparent and every attributed string assign it to the label so plus part of the label will be shown and cursor will be as it is in the textView
I am currently writing my first app on ios using swift 3. I have a plist that has a list of different pieces of information and each is a long paragraph with sections throughout it. for each section, I want to bold the text for the title or at least have the option to format it a certain way rather than just display all the text. I currently have a simple table view that displays text in a text view once tapped. I cannot figure out how to read the paragraph into a string, and compare parts of the string and bold that specific text.
For example, If I had a string that was read from the plist and said:
"My name is #Bob and I like to #dance."
How could I change "#Bob" to "Bob" and "#dance" to "dance" without hard coding it?
#IBOutlet var paragraphTextView: UITextView!
.
.
.
if let text = paragraph["Text"] {
paragraphTextView.text = text
}
The simplest solution is to use some HTML markup in the text in your plist file.
Instead of the plain text:
My name is #Bob and I like to #dance.
Use:
My name is <b>Bob</b> and I like to <b>dance</b>.
This gives you far more flexibility. You can add bold, underline, and italic simply by using the appropriate <b></b>, <u></u>, and <i></i> tags as needed. It also allows you to markup more general ranges than single words.
Once the text has the proper markup, you create an NSAttributedString from the marked up string and then set that to the text viewsattributedTextproperty instead of using thetext` property.
For a good example of how to create an NSAttributedString from HTML text, see HTML Format in UITextView
I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.
So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".
On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.
On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?
You can use this method, Hope it will work for you.
- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {
text = [text stringByReplacingOccurrencesOfString:#":-)" withString:#"😄"];
text = [text stringByReplacingOccurrencesOfString:#";P" withString:#"😜"];
text = [text stringByReplacingOccurrencesOfString:#"B-)" withString:#"😎"];
text = [text stringByReplacingOccurrencesOfString:#";-P" withString:#"😜"];
return text;
}
Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:
In emoticon keyboard:
{
Insert picture into attributed string at location loc;
Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";
}
In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:
{
Parse regular string to jump over emoticon codes already there to find the location matching loc;
Replace text (for instance %101%) in regular string} at the identified location.
}
Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.