Reusing CCLabelTTF - ios

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

Related

Fitting multi-line text into a dynamically size-changing node

A multiline auto typing text box class (which uses an SKNode as the parent) is created using basically 2 elements:
an SKSpriteNode that acts as text box frame & background image/texture holder.
an NSMutableArray containing a set limited amount (rows) of NSStrings that each have a set character length.
After modifying this text box class so that it can be initialized with any frame width & height, I realized I didn't program the NSMutableArray to automatically change its content in a such way that it nicely fits within the background node (with a bit of padding involved as well). So here I am wondering how to do that since NSString's can only return the character count and not the width & height of each string in points (points could have maybe helped me create character constraints in some way).
Right now, the NSMutableArray uses a hardcoded maximum character count per NSString & a maximum row count for the entire array (it's 5 rows right now and when that limit is reached, a new "page"/array is created). This forces me to manually re-adjust these parameters every time I change the background node frame size which defeats the purpose of the class allowing the background frame to change.
Thing is, I'm trying to solve this in such a way that when I post this class on github, I want the solution to take into consideration any fontName & fontSize.
What are my options for solving this problem?
I've done something similar to this. It doesn't work 100% as to what you want, but should be similar enough. It uses a root node and from there, it will build multi-line text using an array of NSString which will in turn be used to build the SKLabelNode.
I'll outline what I did. I should also say I only run this when new text is set. In other words, I do not incur the penalty of deriving the information every frame. Only once.
The generalized steps are:
You will iterate over each character in the text string. Note I do this because my code supports word wrapping as well as other alignment capabilities. So for me, I want that level of control. As this is being done only upon creation, I'm fine with the overhead. If you don't want to word wrap you could always just create an array of words and work from there.
As you iterate over each character, you'll be generating an array of lines. Where each line in the array is a line that will fit in your frame. For now let's not worry about vertical constraints. So here we are primarily worried about width. For the current line, each character you are iterating over will get added to the current line. For this potential line string, you will use NSString's sizeWithAttributes, which is configured for your font. For example in my code it is an NSDictionary which contains: NSFontAttributeName : [UIFont fontWithName:self.fontName size:self.size]. This will be used to check the width, if that width exceeds the frame width, you are overrunning the line.
So the code may look something like:
size = [line sizeWithAttributes:attributes];
if (size.width > maxTextWidth) {
needNewline = YES;
}
If you have overrun a line, you need to determine if you are word wrapping. If you are, you can just add the current line (minus one character) to the lines array. If not you have prune off the last word in the current line and then add that to the array of lines.
The tricky parts are dealing with whitespace and handling non-word wrapped overflow. I have not addressed whitespace but you need to consider this very much in your code. Additionally, you also do want to factor in leading pixels, etc.
Once you have your array of lines, you can then create your children SKLabelNodes. I add them to the root, which allows me to move the group anywhere it needs to be.
The real key here is the lines array generation.

UILabel divides single word

Good day folks,
Actually I'm going crazy, I did everything I could in order to solve this simple problem.
As you see, a simple label in a narrow space causes the single word "Verification" to be separated into two lines which is not acceptable of course.
I know that I could make number of lines only 1 and this will decrease font size, I tried all wraps modules and all fails.
What can I do to display the label as "Verification Process" without any separation of a single word? (I accept even shrinking font size or clipping last word).
The first one is default setting
Does these two settings below meet your needs?
If you are ok with clipping of the words, then you can set the lines property of your label to 1. Or you want to reduce the font size that also can be done using storyboard property inspector. Hope this will help.
please try this.
my testLabel and number of lines equal to 2.
self.testLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.testLabel.text = #"verificaton process";

How to adjust Font Size according text viewable in a TButton.Text using Firemonkey?

I am using buttons to display product names in a matrix using TGridLayout.
The problem is that commonly Items contains 3 or 4 words and in my language (Portuguese) some words tend to be long.
I would like that somehow I could calculate the size of the font, decreasing it, in order to make all the text show up automatically (of course there is also a decrease limit, anything below 9 or 8 point for the font turn to be difficult to read).
The wordwrap property is turned on to have many lines, and use the most possible space for the text.
I don't know if you're programming for an Android/iOS app, but you can't change the fontsize of a button. I've had the same problem, my solution was to make an abbreviation of the words. And then i put labels above it to explain the abbreviations.
Of course you can adjust the font size of a button:
TButton.TextSettings.Font.Size

NSString with different font sizes

Is there a easy way to create the labels text with different font sizes without using NSMutableAttributedString?
label.text=[NSString stringWithFormat:#"%.03f %.03f", var1, var2];
// ^
// this value should be shown a little larger than the second value
Well, you can achieve the look you want by creating three labels, each of a different size. It use to be hard to get the baselines to line up, but if you use the auto layout constraint system, you can get all three labels to hug each other and also have the same baseline.

How to put large text into multiple UITextViews or UILabels?

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.

Resources