C1 Community
ComponentOne Community is a free source for developers and help authors to collaborate and communicate.

Fit Text

rated by 0 users
This post has 4 Replies | 1 Follower

Top 100 Contributor
Posts 51
srodriguez Posted: Tue, Jun 2 2009 7:01 PM

Hello,

 I am trying to render a RenderText object with a set height and width. As I understand, the default value of WordWrap is true, so this takes care of the width. However, if I set the Text property to a string with New Line characters, and the final result of this string has a height larger than the set height of the object, the text gets cut off. How would I go about comparing the size of the RenderText object and the string to decrease the FontSize?

 Thank you!

Top 50 Contributor
Posts 254
I have attached small sample that changes size of font to fit all text.
Top 100 Contributor
Posts 51
Thanks for the sample. However, I couldn't open the project so I just opened the Form1.cs file from notepad. Anyway, this is what I was looking for, and it's helped me to get started, but in my application the FontSize decreases a lot, I think down to 1. Unlike you, I am using UnitTypeEnum.Point, not Cm... could this be it? Is SizeD.Height calculated in Cm or what kind of unit? Also, how does the CalcSize method work, I have read the Doc online but still don't understand it, what does it calculate exactly? Thanks, and sorry for the amount of questions! [EDITED] Sorry, I completely missed 1 line of your sample code. doc.ResolvedUnit which sets the Unit for the objects added to the document. Now the font size doesn't decrease all the way down to 0. However, I still don't understand the CalcSize method, even though I am using it :P... Could you please clarify how this works? I really don't get why we pass those Unit parameters into the method. Thanks again!
Top 50 Contributor
Posts 254

Calculates the size of the current object in units specified by Document.ResolvedUnit
When this method is called, the object must already have been added to the document
(e.g. to C1PrintDocument.Body or to RenderObject.Children of another object in the document).

width - The width of the current object.
This parameter may be specified as Unit.Auto or as an absolute value (but it must not reference other objects).
height - The height of the current object.
This parameter may be specified as Unit.Auto or as an absolute value (but it must not reference other objects).

Returns the size of the current object, in C1PrintDocument.ResolvedUnit units

If the current object has not been added to the document, an exception is thrown.
(This is because to calculate the size of an object, its style is needed, and
due to style inheritance, the effective style of an object depends on the placement
of that object within the document.)
The object may be removed from the document after calling this method if desired.

        /// The following C# code may be used to test whether a RenderText will fit
        /// on the current page if inserted at the current block flow position
        /// (see C1PrintDocument.AvailableBlockFlowHeight):
        C1PrintDocument doc = new C1PrintDocument();
        doc.StartDoc();
        RenderText rt = new RenderText("The text to measure.);
        //add the object temporarily to calculate its size:
        doc.Body.Children.Add(rt);
        // measure the object, specifying page width and auto heigh
        // (i.e. effectively, measure the text height):
        C1PageSettings ps = doc.PageLayout.PageSettings;
        double pageWidth = ps.Width.Value - ps.LeftMargin.Value - ps.RightMargin.Value;
        SizeD sz = rt.CalcSize(new Unit(pageWidth, ps.Width.Units), Unit.Auto);
        // remove the object added temporarily:
        doc.Body.Children.Remove(rt);
        // test whether the object would fit on the page:
        if (doc.AvailableBlockFlowHeight >= sz.Height)
        {
          // object would fit, do something:
          doc.RenderBlockText("YES");
        }
        doc.EndDoc();

Top 100 Contributor
Posts 51

Calculates the size of the current object in units specified by Document.ResolvedUnit
When this method is called, the object must already have been added to the document
(e.g. to C1PrintDocument.Body or to RenderObject.Children of another object in the document).

width - The width of the current object.
This parameter may be specified as Unit.Auto or as an absolute value (but it must not reference other objects).
height - The height of the current object.
This parameter may be specified as Unit.Auto or as an absolute value (but it must not reference other objects).

This makes much more sense! Thank you.

Page 1 of 1 (5 items) | RSS
Contact ComponentOne: 1.800.858.2739 ©1987-2010 ComponentOne LLC All Rights Reserved.