Easy2d 文档教程之常用元素 Text

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Easy2d 文档教程之常用元素 Text脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Text 文本

声明

简易

auto text = new Text(L"你好");

以上代码展示了如何将字符串存入text中。

复杂

auto text = new Text(L"你好", 
					Font(L"宋体", 
						23, 
						Font::Weight::Thin, 
						true), 
					style
				);

以上代码初学者不必完全理解,style指文本样式。

运行起来应该是这样子的:

Easy2d 文档教程之常用元素 Text

原型

explicIT Text(
		const String& text,						/* 文字内容 */
		const Font& font = Font(),				/* 字体 */
		const Style& style = Style()			/* 文本样式 */
	);

getText

获取文本,可以说通过Textconst String变为String 其返回一个String类型

用法

String Gtext = text->getText();//获取text的文本存入Gtext

原型

String getText() const;

getFont

获取文本所用字体,返回一个Font类型 用法

Font font = text->getFont();//存入font

原型

Font getFont() const;

getStyle

获取文本样式,返回一个Style类型

此部分不建议初学者了解,因为涉及Style样式

用法

Style style = text->getStyle();//返回text的Style属性

原型

Style getStyle() const;

getFontFamily

获取字体族,返回一个String类型 用法

auto text = new Text(L"Hello",
					Font(L"宋体", 
						23, 
						Font::weight::Thin,
						false)
				 	);
String family = text->getFontFamily();//返回“宋体”

原型

String getFontFamily() const;

getFontSize

获取字号,返回float 用法

auto text = new Text(L"Hello",
					Font(L"宋体", 
						23, 
						Font::weight::Thin,
						false)
				 	);
float size = text->getFontSize();//返回23.0

getFontWeight

获取当前字体粗细值,返回UNIT类型 枚举类型Weight原型:

enum Weight : UINT
{
	Thin = 100,
	ExtraLight = 200,
	Light = 300,
	Normal = 400,
	Medium = 500,
	Bold = 700,
	ExtraBold = 800,
	Black = 900,
	ExtraBlack = 950
};

用法

//使用tyPEdef定义UNIT方便理解
typedef unsigned int UNIT;//UNIT 指 UNsigned InT
auto text = new Text(L"Hello",
					Font(L"宋体", 
						23, 
						Font::weight::Thin,
						false)
				 	);
UNIT weight = text->getFontWeight();//值为100

//同理,weight的值同样做来当ENUM类型Weight的替代值
Font font = new Font(L"宋体", 23, weight, false);

原型

UINT getFontWeight() const;

参见:ENUM枚举类型

getColor 与 getOutlineColor

分别获取文字和描边颜色,返回Color类型。 Color类型参见:Easy2d 文档教程之基础类型 上

用法

Color color = text->getColor();
Color color = text->getOutlineColor();

原型

// 获取文字颜色
Color getColor() const;

// 获取描边颜色
Color getOutlineColor() const;

getOutlineWidth

获取描边线,返回float类型 用法:

float width = text->getOutlineWidth();
//描线边框

原型

// 获取描边线宽
float getOutlineWidth() const;

getOutlineJoin

获取描边线相交样式,返回LineJoin类型 此部分不建议初学者深入,因为涉及LineJoinStyle类型 适合配合setOutlineJoin使用 LineJoin原型:

enum class LineJoin : int//线条交叉样式
{
	Miter = 0,	/* 斜切 */
	Bevel = 1,	/* 斜角 */
	Round = 2	/* 角 */
};

用法

LineJoin line1 = LineJoin::Miter;//斜切

Text::Style a = Text::Style(
								Color(Color::Black), 
								Text::Align::Left, 
								false, 
								0.0,
								0.0,
								false,	
								false,
								true,
								Color(Color::red), 
								2.0, 
								line1
						);
auto text = new Text(L"你好",Font(),a);
LineJoin line2 = text->getOutlineJoin();//返回 LineJoin::Miter

原型

// 获取描边线相交样式
LineJoin getOutlineJoin() const;

getLineCount

获取文本显示行数,配合自动换行setWordWrappingEnable 使用,返回int类型 用法

auto text = new Text(L"Hello Easy2d!");

text->setWordWrappingEnable(true);  // 开启自动换行
text->setWordWrappingWidth(70);     // 设置文字自动换行的宽度
text->getLineCount();//返回 3

Easy2d 文档教程之常用元素 Text

原型

int getLineCount() const;

isItalic

是否斜体,配合Font使用,返回一个bool类型 用法

auto text = new Text(L"你好", Font(L"宋体", 22, Font::Weight::Normal, ture));
text->isItalic();//返回 true

原型

bool isItalic() const;

hasStrikethrough、hasUnderline 和 hasOutline

分别检测是否显示删除线、下划线和描边,返回bool类型,配合Style使用。 用法

Text::Style a = Text::Style(
								Color(Color::Black), 
								Text::Align::Left, 
								true, 
								0.0,
								0.0,
								true,	
								true,
								true,
								Color(Color::Red), 
								2.0, 
								LineJoin::Round
						);
text->hasStrikethrough();//返回true
text->hasUnderline();//返回true
text->hasOutline();//返回true

原型

// 是否显示删除线
bool hasStrikethrough() const;

// 是否显示下划线
bool hasUnderline() const;

// 是否显示描边
bool hasOutline() const;

setText、setStyle 和 setFont

分别设置文本、样式、字体。 用法

auto text = new Text();
text->setText(L"你好");//你要的文本
text->setStyle(Style());//你要的样式
text->setFont(Font());//你要的字体

原型

// 设置文本
void setText(
	const String& text
);

// 设置文本样式
void setStyle(
	const Style& style
);

// 设置字体
void setFont(
	const Font& font
);

setFontFamily、setFontSize、setFontWeight

属于派生类Font的参数设置 分别设置字体族、字号和粗细值 用法

auto text = new Text();
text->setText(L"你好");//你要的文本
text->setFontFamily(L"宋体");//设置字体族为宋体
text->setFontSize(22);//默认字号
text->setFontWeight(Font::Weight::Normal);//正常粗细

原型

// 设置字体族
void setFontFamily(
	const String& family
);

// 设置字号(默认值为 22)
void setFontSize(
	float size
);

// 设置字体粗细值(默认值为 Text::Font::Weight::Normal)
void setFontWeight(
	UINT weight
);

setColor

设置文本颜色 用法

auto text = new Text(L"你好");
text->setColor(Color::Red);//红色的文本

原型

// 设置文字颜色(默认值为 Color::WHITE)
void setColor(
	Color color
);

setItalic

设置文本斜体,true为打开,false为关闭。 用法

text->setItalic(true);//斜体

原型

// 设置文字斜体(默认值为 false)
void setItalic(
	bool value
);

setWrappingWidth 和 setWrapping

分别设置自动换行宽度和自动换行。 用法

text->setWrapping(true);//先开启
text->setWrappingWidth(70);//调整宽度

原型

// 打开或关闭文本自动换行(默认为关闭)
void setWrapping(
	bool wrapping
);

// 设置文本自动换行的宽度(默认为 0)
void setWrappingWidth(
	float wrappingWidth
);

setLineSpacing

设置行间距,默认0 用法

text->setLineSpacing(0.3);//0.3的间距

原型

// 设置行间距(默认为 0)
void setLineSpacing(
	float lineSpacing
);

setAlignment

设置对齐方式(默认为 Align::Left) 用法

text->setAlignment(Align::Left);//左
text->setAlignment(Align::Right);//右
text->setAlignment(Align::center);//居中

原型

// 设置对齐方式(默认为 Align::Left)
void setAlignment(
	Align align
);
// 文本对齐方式
enum class Align
{
	Left,		/* 左对齐 */
	Right,		/* 右对齐 */
	Center		/* 居中对齐 */
};

setUnderline和setStrikethrough

分别设置文本的下划线和删除线 用法

text->setUnderline(1);//显示下划线
text->setStrikethrough(1);//显示描边
//参数为0表示不显示

原型

// 设置下划线(默认值为 false)
void setUnderline(
	bool hasUnderline
);

// 设置删除线(默认值为 false)
void setStrikethrough(
	bool hasStrikethrough
);

setOutline、setOutlineColor和setOutlineWidth

分别设置文本描边、描边颜色、描边宽度

text->setOutline(1);//开启描边
text->setOutlineColor(Color::Red);//设置为红色
text->setOutlineWidth(1.1);//线宽1.1

原型

// 设置是否显示描边
void setOutline(
	bool hasOutline
);

// 设置描边颜色
void setOutlineColor(
	Color outlineColor
);

// 设置描边线宽
void setOutlineWidth(
	float outlineWidth
);

setOutlineJoin

设置描边线相加样式。 用法

text->setOutlineJoin(LineJoin::Miter);//斜切
text->setOutlineJoin(LineJoin::Bevel);//斜角
text->setOutlineJoin(LineJoin::Round);//圆角

原型

// 线条相交样式
enum class LineJoin : int
{
	Miter = 0,	/* 斜切 */
	Bevel = 1,	/* 斜角 */
	Round = 2	/* 圆角 */
};
// 设置描边线相交样式
void setOutlineJoin(
	LineJoin outlineJoin
);

结束语

因为学业原因,拖更了很久,实在抱歉! 同步发布链接:知乎,博客园,CSDN

脚本宝典总结

以上是脚本宝典为你收集整理的Easy2d 文档教程之常用元素 Text全部内容,希望文章能够帮你解决Easy2d 文档教程之常用元素 Text所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。