Com formatar text en C++

Com formatar text en C++

Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat classe. A Portion és la unitat independent més petita de text; es correspon a una única execució de format dins d’un paràgraf. Aquesta guia mostra com aplicar format en negreta, cursiva, mida de lletra i color al text en una presentació.

Guia pas a pas

Pas 1: Compilar i enllaçar la biblioteca

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

Pas 2: Afegeix una forma amb un marc de text

Abans de formatar el text, afegeix una forma i estableix el seu contingut de text mitjançant shape.text_frame()->set_text().

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Default text: will be formatted");
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Pas 3: Accedeix al TextFrame

shape.text_frame() retorna un punter al TextFrame. Utilitza -> per cridar mètodes sobre ell.

auto* tf = shape.text_frame();          // pointer to the shape's text frame
tf->set_text("your text here");

Una TextFrame conté Paragraph objectes (tf->paragraphs()). Cada Paragraph conté Portion objectes (paragraph.portions()).


Pas 4: Aplica format de negreta i cursiva

Utilitzeu portion_format().set_font_bold() i portion_format().set_font_italic(). Aquests mètodes accepten NullableBool::TRUE, NullableBool::FALSE, o NullableBool::NOT_DEFINED (heretat del mestre).

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Bold and italic text");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.set_font_italic(asf::NullableBool::TRUE);

    prs.save("bold-italic.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Pas 5: Estableix la mida de la lletra i el color

Estableix portion_format().set_font_height() per a la mida (en punts) i utilitzeu fill_format() per al color.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Large corporate-blue heading");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_height(32);                               // 32pt font
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.fill_format().set_fill_type(asf::FillType::SOLID);
    fmt.fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 0, 70, 127));

    prs.save("colored-text.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Color::from_argb(alpha, red, green, blue) accepta valors de 0-255 per a cada canal.


Pas 6: Múltiples porcions en un mateix paràgraf

Un únic paràgraf pot contenir diverses porcions amb formatació diferent. Afegeix un nou Portion a un paràgraf portions() col·lecció:

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 600, 100);
    shape.text_frame()->set_text(""); // start with empty text
    auto* tf = shape.text_frame();

    auto& paragraph = tf->paragraphs()[0];

    // First portion: normal text
    auto& portion1 = paragraph.portions()[0];
    portion1.set_text("Normal text followed by ");
    portion1.portion_format().set_font_height(20);

    // Second portion: bold red text
    asf::Portion portion2;
    portion2.set_text("bold red text");
    portion2.portion_format().set_font_height(20);
    portion2.portion_format().set_font_bold(asf::NullableBool::TRUE);
    portion2.portion_format().fill_format().set_fill_type(asf::FillType::SOLID);
    portion2.portion_format().fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 200, 0, 0));
    paragraph.portions().add(portion2);

    prs.save("mixed-format.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Problemes comuns i solucions

El text apareix negre fins i tot després d’establir el color

Assegureu-vos fill_format().set_fill_type(FillType::SOLID) està establert abans d’assignar el color. Sense establir el tipus d’emplenament, el canvi de color pot no tenir cap efecte.

NullableBool::TRUE vs true

portion_format().set_font_bold() espera NullableBool::TRUE, no el C++ true. Passant true directament no compilarà o tindrà un comportament indefinit segons la resolució de sobrecàrrega.

La font no apareix al fitxer desat

El set_latin_font() mètode estableix la família de fonts llatines. Si no s’estableix, s’utilitza la font del tema de la presentació. Les fonts personalitzades han d’estar incrustades o disponibles a la màquina de visualització.


Preguntes freqüents

Com canvio la família de fonts?

Estableix portion_format().set_latin_font():

fmt.set_latin_font(asf::FontData("Arial"));

FontData accepta el nom de la família tipogràfica com a cadena.

Com estableixo l’alineació del paràgraf?

Ús paragraph_format().set_alignment():

tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);

Valors admesos: LEFT, CENTER, RIGHT, JUSTIFY.

Com estableixo l’interlineat?

Ús paragraph_format().set_space_before() (punts abans del paràgraf) o paragraph_format().set_space_after() (punts després del paràgraf):

tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6);   // 6pt after

Vegeu també

 Català