Narendrabrsoft / cocos2d-android-1

Automatically exported from code.google.com/p/cocos2d-android-1
0 stars 0 forks source link

No line-breaks in CCLabel #70

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Text with line-breaks for CCLabel.

What is the expected output? What do you see instead?
CCLabel ignores Linebreaks

What version of the product are you using? On what operating system?
latest from gid

Please provide any additional information below.
The iphone-version of cocos2d works perfectly with line-breaks in CCLabel.

Original issue reported on code.google.com by erdenkri...@googlemail.com on 19 May 2011 at 4:30

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Yeah it should not sure why I didn't include earlier. Check the last comment 
the code that you can paste into CCTexture2D if you don't want to wait for it 
to get merged into project.
https://github.com/ZhouWeikuan/cocos2d/pull/11

Original comment by thomashi...@gmail.com on 21 May 2011 at 3:31

GoogleCodeExporter commented 8 years ago
I discovered, that's not enough to include code from comment above.
In case, when you use 
CCLabel.makeLabel (string, fontname, fontsize)
or
CCLabel.makeLabel (string, dimensions, aligment, fontname, fontsize) where 
"dimensions" is CGSize.make(0,0)

you get only one-line text on display, because in CCTexture2D.calculateTextSize 
text height calculated only for one-line string.
For correct this necessary multiple one-line height to lines amount.

Old code is (CCTexture2D.calculateTextSize)
return CGSize.make(measuredTextWidth, ascent + descent);
correct code is
return CGSize.make(measuredTextWidth, (ascent + 
descent)*text.split("\n").length);

Can someone check this and include in main stream?

And one more:
After all changes, if I call
CCLabel.makeLabel("line1\nline2", CGSize.make(0, 0), TextAlignment.LEFT, 
"DroidSans", 24);
I get right text, but with center align (TextAlignment.CENTER).
That's because in CCLabel.StringReloader.load(res) in case, when I set 
CGSize(0,0) it force call
CCTexture2D.initWithText(string, fontname, fontsize) with default aligment 
TextAlignment.CENTER
I don't know, this bug or feature, but I can't place label with automatic 
dimension calc and left align.

Original comment by max.gaz...@gmail.com on 27 Oct 2011 at 3:04

GoogleCodeExporter commented 8 years ago
I lost to correct width - necessary calculate it only for one longest line.
Full correct code CCTexture2D.calculateTextSize method is:

    private static CGSize calculateTextSize(String text, String fontname, float fontSize) {
        Typeface typeface;
        if(!typefaces.containsKey(fontname)) {
            try {
                CCDirector.theApp.getAssets().open(fontname);
                typeface = Typeface.createFromAsset(CCDirector.theApp.getAssets(), fontname);
            } catch(IOException e) {
                typeface = Typeface.create(fontname, Typeface.NORMAL);
            }
            typefaces.put(fontname, typeface);
        } else {
            typeface = typefaces.get(fontname);
        }

        Paint textPaint = new Paint();
        textPaint.setTypeface(typeface);
        textPaint.setTextSize(fontSize);
        textPaint.setAntiAlias(true);
        String[] textLines = text.split("\n");
        String maxLine = "";
        for (String s : textLines){
            if (maxLine.length() < s.length()){
                maxLine = s;
            }
        }
        int ascent = (int) Math.ceil(-textPaint.ascent());  // Paint.ascent is negative, so negate it
        int descent = (int) Math.ceil(textPaint.descent());
        int measuredTextWidth = (int) Math.ceil(textPaint.measureText(maxLine));

        return CGSize.make(measuredTextWidth, (ascent + descent)*textLines.length);
    }

Original comment by max.gaz...@gmail.com on 28 Oct 2011 at 11:52