I am facing a problem placing a large text into multiple UITextView/UILabel.
Example: Please see the attached picture ,it explains my problem.
assume three boxes are UITextViews or UILabels. Text will get from server as a complete one NSString, Now challenge is to place the text in the boxes (UITextView/UILabel).
could anyone please help me in solving this problem.
A UIWebView might be more suitable for the task of laying out a page. You could then use html with CSS floats to achieve the effect you're looking for.
If you do not want to use UIWebView, I suggest looking at Core Text for layout.
If you have bounding box for those elements, than you can easily implement this logic by iterating usage of method - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode and removing by one word from string till returned size will fit into bounding box for corresponding section, than truncate text to place where you stop removing words and complete with other sections.
Related
I want to use the very same label in all my CCMenuItems. If I create the same CCLabelTTF once, then I can't add it to more than one CCMenuItem because it will give runtime error about label already added. But creating the same label many times also not effective, if the label is same. How to optimally solve this problem?
you cannot use the same label more than once, the label is not only what you see, for example it also contain a position, so you cannot place the same item in 2 different points.
What's the problem on creating more than one? if you have a LOT of ttf labels that change text often you can consider using bitmap fonts. they are rendered faster
I need to render an (arbitrarily large) NSAttributedString, in this case ANSI-colored text from an (arbitrarily long) telnet session. The text need not be editable inline. I have explored a few options:
UITextView seems to have by far the best performance and, since I'm targeting iOS 6, it's very easy to use with attributed strings. However, the textview gets progressively slower to render as more text is added, as it hits an HTML DOM parser each time I call setAttributedString: and blocks the UI.
I've tried a few core text rendering frameworks, TTTAttributedLabel and OHAttributedLabel, that also get progressively slower with more text. To be fair, they're labels probably not intended for this sort of thing!
UIWebView (gag) has some issues with rotation and keeping the text properly sized and framed, but I think I could work around it. I can convert my attributed string to HTML and use JavaScript to append (inject) new text as it is received. Surprisingly good performance here.
A friend suggested I think of the user's current scroll position as a viewport into a larger document and (probably with core text) render only the visible part of my attributed string. I'm worried about how this might impact scrolling performance.
So I turn to you, brave interwebs. Ideas for an indie developer? Is a webview my best bet?
You could use a UITableView and split the NSAttributedString into an array of substrings that would each fit a cell's label width. The table view's data source would index into the array of substrings to determine which line of the original string should be placed in each cell.
I'm attempting to draw a richly laid out text view on iPhone that features:
Custom paragraph spacing (kCTParagraphStyleSpecifierParagraphSpacing)
Custom paragraph first-line indentation (kCTParagraphStyleSpecifierFirstLineHeadIndent)
Justified alignment (kCTParagraphStyleSpecifierAlignment)
Finally, a drop cap on my first paragraph
I'm using OHAttributedLabel. The first three points I achieved without much trouble by just setting some paragraph style attributes on my NSAttributedString.
The drop cap I managed to implement by hacking OHAttributedLabel:
Cut out a rectangular region out of the main paragraph's CGMutablePathRef the size of the drop cap by adding an extra CGPathAddRect, as detailed in this excellent blog post.
Drawing the large character in this region with an extra CTFrameDraw call.
My problem: The paragraph styles and the custom text path are incompatible. When I cut a rectangular chunk out of the main text's path, all the paragraph styles seem to get thrown away.
Does anyone know a way to make them work together? Or can anyone think of another way to implement drop caps? (Short of using a UIWebView + CSS, which I'd rather not have the overhead of!)
Thanks!
You can use straight Core Text to achieve this, in the following post I explain the use of 2 framesetters to lay out text with drop caps in a UIView. In the code example (there's also a link to a github repo) you'll be able to see where the paragraph styles are created and applied to the main text view.
https://stackoverflow.com/a/14639864/1218605
I'm puzzling how to divine more font metrics from iOS. I can accomplish my very long single line of text by paging UIViews while scrolling. I can determine where to break the strings between views by iteratively calculating it with sizeToFont, but I need the inter-character spacing (i.e. advance width) to space them accurately.
Any ideas short of full fixed or monospaced fonts? Thanks.
The Core Text class CTFont gives you some access to glyph metrics. But really, you should try to just use Core Text to do your text rendering instead of asking it for measurements and calculating positions yourself.
CTFont Reference
Core Text Programming Guide
How can I fully justify a block of text (like MS Word does, not only on the right and not only on the left but on both sides)?
I want to justify some texts (mainly arabic text) adjusted to certain screen size (some handheld device screen actually, and its text viewer doesn't have this function) and save this text as justified. So I can reload and reuse it again elsewhere.
(The problem with MS word is, that if you copy the justified text from MS Word and paste it to another editor it'll copy it un-justified).
Update : for now I'm thinking of doing it like this:
get-a-word
get-word-width
add-word-to-total-Word and add-Word-width-to-total-word-width
check if total-Word-width = myscreen-width then continue
else if total-Word-width is between myscree-wdith and (myscreen-width -3) then
add-spaces-To-total-word until it = myscreen-width
This is what I'm thinking now, but I put this question up and hope to see if there is a better solution, or somebody else already implemented it.
PS: I hope I have made my question clear and I'm sorry for bad expression if there is.
edit1 : changed the title to make it more clear.
If you want to justify plain text, you can only add extra spaces to the lines to get them align on the left and right. Unfortunately the character widths differ in fonts; so doing it this way will only work for a certain font, unless you limit yourself to monospaced fonts where all characters have the same size.
If you want a result like in Word, adding spaces won't cut it. Word will not add spaces, but stretch and shrink the existing spaces. This information is lost when you copy and paste it into another app.
Either way, justifying is an optimization problem. If you are interested in a good solution and its implementation: have a look a TeX. For an implementation that works on plain text with monospaced fonts have a look at par
There are some API calls that may help:
ExtTextOut and GetCharacterPlacement
Look at the GCP_JUSTIFY flag for GetCharacterPlacement
ExtTextOut is used by Canvas.TextRect
The problem you are going to face is always going to be differences in the rendering of the font. Word handles full justification by adjusting kerning as well as adjusting the number of pixels between words by a few (either way). The end result is lined up both margins. This pixel adjustment is done BOTH ways, and as evenly as possible.
To properly handle this in your portable device you will have to also perform the same algorithm for the display of the text there.
If this is not possible, then the ONLY way you can even get somewhat close would be to add whitespace between words.
As has been pointed out in other answers Word does full justification by stretching the existing spaces often by very small amounts. This is only possible if you have full control over how your text is drawn on the screen (which word - or any other windows program has).
You only real option in this regard would be to implement your own text viewer on the platform you are targeting. Eg you would need to draw the text on the screen yourself (any platform that allows games should allow you to draw on the screen). However this seems like an awful lot of trouble to get justified text.
Sorry couldn't be of more help.