Jak formatować tekst w C++

Jak formatować tekst w C++

Aspose.Slides FOSS for C++ zapewnia precyzyjne formatowanie tekstu za pomocą klasy PortionFormat. Portion jest najmniejszą niezależną jednostką tekstu; odpowiada pojedynczemu zakresowi formatowania w akapicie. Ten przewodnik pokazuje, jak zastosować pogrubienie, kursywę, rozmiar czcionki i formatowanie koloru do tekstu w prezentacji.

Przewodnik krok po kroku

Krok 1: Zbuduj i połącz bibliotekę

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 .

Krok 2: Dodaj kształt z ramką tekstową

Przed formatowaniem tekstu dodaj kształt i ustaw jego zawartość tekstową za pomocą 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;
}

Krok 3: Uzyskaj dostęp do TextFrame

shape.text_frame() zwraca wskaźnik do TextFrame kształtu. Użyj ->, aby wywołać na nim metody.

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

TextFrame zawiera Paragraph obiektów (tf->paragraphs()). Każdy Paragraph zawiera Portion obiektów (paragraph.portions()).


Krok 4: Zastosuj pogrubienie i kursywę

Użyj portion_format().set_font_bold() i portion_format().set_font_italic(). Te metody akceptują NullableBool::TRUE, NullableBool::FALSE lub NullableBool::NOT_DEFINED (dziedziczą po masterze).

#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;
}

Krok 5: Ustaw rozmiar i kolor czcionki

Ustaw portion_format().set_font_height() dla rozmiaru (w punktach) i użyj fill_format() dla koloru.

#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) akceptuje wartości 0‑255 dla każdego kanału.


Krok 6: Wiele części w jednym akapicie

Pojedynczy akapit może zawierać wiele fragmentów o różnym formatowaniu. Dodaj nowy Portion do kolekcji portions() akapitu:

#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;
}

Typowe problemy i rozwiązania

Tekst wyświetla się czarny mimo ustawienia koloru

Upewnij się, że fill_format().set_fill_type(FillType::SOLID) jest ustawiony przed przypisaniem koloru. Bez ustawienia typu wypełnienia zmiana koloru może nie przynieść efektu.

NullableBool::TRUE vs true

portion_format().set_font_bold() oczekuje NullableBool::TRUE, a nie C++ true. Przekazanie true bezpośrednio nie zostanie skompilowane lub będzie miało niezdefiniowane zachowanie w zależności od rozstrzygnięcia przeciążenia.

Czcionka nie pojawia się w zapisanym pliku

Metoda set_latin_font() ustawia rodzinę czcionek łacińskich. Jeśli nie zostanie ustawiona, używana jest czcionka motywu prezentacji. Czcionki niestandardowe muszą być osadzone lub dostępne na maszynie wyświetlającej.


Najczęściej zadawane pytania

Jak zmienić rodzinę czcionki?

Ustaw portion_format().set_latin_font():

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

FontData akceptuje nazwę rodziny czcionki jako ciąg znaków.

Jak ustawić wyrównanie akapitu?

Użyj paragraph_format().set_alignment():

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

Obsługiwane wartości: LEFT, CENTER, RIGHT, JUSTIFY.

Jak ustawić interlinię?

Użyj paragraph_format().set_space_before() (punkty przed akapitem) lub paragraph_format().set_space_after() (punkty po akapicie):

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

Zobacz także

 Polski