איך לעצב טקסט ב‑C++
Aspose.Slides FOSS for C++ מספקת עיצוב טקסט מדויק דרך המחלקה PortionFormat. Portion הוא היחידה העצמאית הקטנה ביותר של טקסט; הוא ממופה לרצף עיצוב יחיד בתוך פסקה. מדריך זה מראה כיצד להחיל עיצוב מודגש, נטוי, גודל גופן וצבע על טקסט במצגת.
מדריך שלב אחר שלב
שלב 1: בנייה וקישור הספרייה
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 .שלב 2: הוסף צורה עם מסגרת טקסט
לפני עיצוב הטקסט, הוסף צורה והגדר את תוכן הטקסט שלה באמצעות 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;
}שלב 3: גישה ל‑TextFrame
shape.text_frame() מחזיר מצביע לTextFrame של הצורה. השתמש ב-> כדי לקרוא לשיטות עליו.
auto* tf = shape.text_frame(); // pointer to the shape's text frame
tf->set_text("your text here");TextFrame מכיל Paragraph אובייקטים (tf->paragraphs()).
כל Paragraph מכיל Portion אובייקטים (paragraph.portions()).
שלב 4: החל עיצוב מודגש ונטוי
השתמש ב-portion_format().set_font_bold() וב-portion_format().set_font_italic(). שיטות אלו מקבלות NullableBool::TRUE, NullableBool::FALSE או NullableBool::NOT_DEFINED (יורשות מהמאסטר).
#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;
}שלב 5: הגדר גודל גופן וצבע
הגדר portion_format().set_font_height() לגודל (בנקודות) והשתמש בfill_format() לצבע.
#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) מקבל ערכים 0-255 עבור כל ערוץ.
שלב 6: חלקים מרובים בפסקה אחת
פסקה בודדת יכולה להכיל מספר חלקים עם עיצוב שונה. הוסף Portion חדש לאוסף portions() של פסקה:
#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;
}בעיות נפוצות ותיקונים
הטקסט מופיע שחור גם אחרי קביעת צבע
ודא ש-fill_format().set_fill_type(FillType::SOLID) מוגדר לפני הקצאת הצבע. ללא הגדרת סוג המילוי, שינוי הצבע עשוי שלא להיות בעל השפעה.
NullableBool::TRUE לעומת true
portion_format().set_font_bold() מצפה לNullableBool::TRUE, ולא לtrue של C++. העברת true ישירות לא תקומפל או תגרום להתנהגות בלתי מוגדרת, תלוי ברזולוציית העומס.
הגופן אינו מופיע בקובץ השמור
המתודה set_latin_font() מגדירה את משפחת הגופנים הלטינית. אם לא הוגדר, נעשה גופן ערכת הנושא של המצגת. גופנים מותאמים אישית חייבים להיות משובצים או זמינים במחשב הצפייה.
שאלות נפוצות
איך אני משנה את משפחת הגופנים?
הגדר portion_format().set_latin_font():
fmt.set_latin_font(asf::FontData("Arial"));FontData מקבל את שם משפחת הגופן כמחרוזת.
איך אני מגדיר יישור פסקה?
השתמש paragraph_format().set_alignment():
tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);ערכים נתמכים: LEFT, CENTER, RIGHT, JUSTIFY.
איך אני מגדיר ריווח שורות?
השתמש בparagraph_format().set_space_before() (נקודות לפני פסקה) או בparagraph_format().set_space_after() (נקודות אחרי פסקה):
tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6); // 6pt after