איך לעצב טקסט ב‑C++
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat מחלקה. A 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");A 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, ולא את C++ true. העברה 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