style~TextSymbol(options)

文本符号。
注意:
1、当使用文字倾斜或旋转功能时,出现文字被切角的情况时,请参考矢量图层的说明文档。
2、线文字不支持背景相关的配置项

Summary

Methods:
clone

克隆一个新的文本符号。

getBackgroundColor

获取背景填充颜色。

getBackgroundImage

获取文字样式背景图片

getFontColor

获取填充颜色。

getFontFace

获取字体的字族,比如'宋体'。

getFontSize

获取字体大小。

getFontStyle

获取字体类型eg:斜体。

getFontWeight

获取字体粗细。

getHaloColor

获取描边颜色。

getHaloWidth

获取描边宽度。

getLineRenderData

创建线文字图集,返回纹理坐标,

getLineRenderData2

临时使用 勿删

getOffset

获取偏移量。

getOpacity

获取透明系数。

getOriginText

获取原始文本内容。当原始文本太长时,会被截取展示。

getPaddingBottom

获取文字的底部内衬宽度

getPaddingLeft

获取文字的左边内衬宽度

getPaddingRight

获取文字的右边内衬宽度

getPaddingTop

获取文字的顶部内衬宽度

getRotation

获取旋转角度。

getRowTextCount

获取每行文字个数的最大值。

getRowWidth

固定文字宽度的模式下,获取每行宽度。

getShadowColor

获取文字样式的阴影颜色

getShadowOffset

获取文字样式的阴影偏移量

getText

获取文本内容。

getTextAlign

已废弃,建议使用getTextAlignment代替。用于获取文本水平排列方式。

getTextAlignment

获取文本水平排列方式。

getTextBaseline

获取文本基线,即垂直排列的方式。

setBackgroundColor

设置背景填充颜色。

setBackgroundImage

设置文字样式背景图片

setFontColor

设置字体的颜色。

setFontFace

设置字体族名称。参数fontFace即css中的fontFamily,但是只支持单个字体族名称,不支持多个字体族组成的列表。
默认值是“Microsoft YaHei”,其他可取的值包括:"arial"、"serif"、"sans-serif"、"monospace"、"fantasy"、"cuisive"、"宋体"、"微软雅黑"、"华文细黑"、"黑体"等。

setFontSize

设置字体大小。最小值为8,最大值为64.

setFontStyle

设置字体样式。默认是“normal”,可取的值包括“normal”、“italic”、“oblique”。
1、normal 默认值。浏览器显示标准的字体样式。
2、italic 浏览器会显示一个斜体的字体样式。
3、oblique 浏览器会显示一个倾斜的字体样式。

setFontWeight

设置字体粗细,默认是"normal"。可以传入的值包括:"normal"、"bold"、"bolder",传入其它异常值,会导致不可预估的错误!

setHaloColor

设置字的描边颜色。

setHaloWidth

设置描边宽度。

setOffset

设置文字的偏移量。

setOpacity

设置文字整体的透明系数。

setRotation

设置旋转角度。

setRowTextCount

设置每行文字个数的上限。

setText

设置文本内容。

setTextAlign

已废弃,建议使用setTextAlignment代替。用于设置文本水平排列方式,可选项包括'left','right','center'。

setTextAlignment

设置文本水平排列方式。

setTextBaseline

设置文本基线。

Constructor

new TextSymbol(options)

构造函数。

Parameters:
Name Type Description
options Object

文本符号的可选属性。

Name Type Description
fontWeight String

字体的粗细,默认值为'normal'。其他可选值为‘bold’和'bolder'。

fontStyle String

字体类型,默认为'normal'。

fontSize Number

字体大小,默认值为20,最小值为8,最大值为64。

fontFace String

字体族 与ccs中的font-family相同。默认值为'Microsoft YaHei'。

text String

要显示的文字内容。如果要根据要素中属性字段名确定内容,请使用‘{name}’这种格式定义,name为字段名。如果文字内容包含换行符'\n',将依据换行符进行换行;如果不包含换行符,则依据参数rowTextCount进行换行。

offset module:basetype~Offset

文字符号的偏移量,单位是像素。

textAlign String

Deprecated:已废弃,建议使用textAlignment代替。文字的水平排列方式,默认值为'center',其他可选值为'left'、'right'。

textAlignment String

文本水平对齐方式,默认值为'center':居中排列,其他可选值为'left':左对齐、'right':右对齐。

textBaseline String

锚点在文本垂直方向上位置,默认值为'middle':锚点在中间,其他可选值为'top':锚点在文本顶部、'bottom':锚点在文本底部。

fontColor module:style~Color

字体颜色值,默认为黑色。

haloColor module:style~Color

字体描边颜色,默认为完全透明的白色。

haloWidth Number

描边的宽度,单位为像素,默认为0。

backgroundColor module:style~Color

字体的背景颜色 默认为完全透明的白色。

backgroundImage String | Image

文字背景图片,可以是图片路径,也可以是Image对象

shadowOffset Array.<Number>

阴影在横向和纵向上的偏移,单位像素,默认值为[0,0]

shadowBlur Number

阴影宽度,单位像素,默认值为0

shadowColor module:style~Color

阴影颜色,默认为透明,即没有阴影。

rotation Number

旋转角度,单位是度,正常值在-360~360度之间。角度为正值进行逆时针旋转;角度为负值进行顺时针旋转。

rowTextCount Number

单行文字的个数,默认为6,最小值为1,最大值为10

rowWidth Number

单行文字的宽度,单位为像素,默认为0。优先级高于rowTextCount,如果设置了单行文字宽度不为0,单行文字个数则不生效。

opacity Number

透明度。

padding Number

背景内衬宽度,单位为像素,默认为0

paddingLeft Number

背景左边内衬宽度,单位为像素,优先级大于options.padding,如果不设置,默认使用options.padding的值

paddingRight Number

背景右边内衬宽度,单位为像素,优先级大于options.padding,如果不设置,默认使用options.padding的值

paddingTop Number

背景顶部内衬宽度,单位为像素,优先级大于options.padding,如果不设置,默认使用options.padding的值

paddingBottom Number

背景底部内衬宽度,单位为像素,优先级大于options.padding,如果不设置,默认使用options.padding的值

anchor Array.<Number>

锚点位置。默认将锚点定为整个文字的中心点,即[0.5,0.5]。左上角为[0,0],右下角为[1,1]。

drawComplex Boolean

是否支持复杂的背景,默认为false。不能和backgroundImage同时使用,建议使用背景图片,这样可以极大降低图集撑爆的风险。

backgroundCornerRadius Number

背景圆角半径,单位为像素,默认为0,直角。仅在drawComplex为true时起作用。

backgroundStrokeColor module:style~Color

背景描边颜色,默认为透明,不描边。仅在drawComplex为true时起作用。

backgroundStrokeWidth Number

背景描边宽度,单位为像素,默认为0,不描边。仅在drawComplex为true时起作用。

Example
let textSymbol = new hmap.style.TextSymbol({
    fontWeight: 'normal',//字的粗细
    fontStyle: "normal",//斜体等
    fontSize: 16,//字大小
    fontFace: "Microsoft YaHei",//字体
    text: "江南大道",
    offset: new hmap.basetype.Offset(1,1,0),
    placement: "point",//文本的放置方式
    rotateWithView: false,//是否跟着视图旋转
    rotation: 0,//旋转角度,单位为度
    textAlignment: "center",//文本水平排列方式,分为center、left、right,end,start
    textBaseline: "top",//文本垂直排列方式,分为top、middle、bottom
    overflow: false,//是否允许文本超出多边形或线的路径长度
    fontColor: new hmap.style.Color(6,6,6,1),//字体填充颜色
    haloColor: new hmap.style.Color(255,1,1,1),//字体描边颜色
    haloWidth: 1,//字体描边宽
    backgroundColor: new hmap.style.Color(157,200,247,1),//背景填充颜色
    maxAngle: Math.PI/4,   //允许相邻文字之间的最大角度,单位弧度
    opacity: 0.5
});

Methods

clone() → {module:style~TextSymbol}

克隆一个新的文本符号。

Returns:
module:style~TextSymbol -

文本符号对象

Example
let newTextSymbol = textSymbol.clone();

getBackgroundColor() → {module:style~Color}

获取背景填充颜色。

Returns:
module:style~Color -

背景颜色值。

Example
let backgroundColor = textSymbol.getBackgroundColor();

getBackgroundImage() → {Image|Promise}

获取文字样式背景图片

Returns:
Image | Promise -

返回文字背景图片,或者请求该背景图片的Promise

getFontColor() → {module:style~Color}

获取填充颜色。

Returns:
module:style~Color -

字体颜色值。

Example
let fontColor = textSymbol.getFontColor();

getFontFace() → {String}

获取字体的字族,比如'宋体'。

Returns:
String -

返回字体的字族。

Example
let fontFace = textSymbol.getFontFace();

getFontSize() → {Number}

获取字体大小。

Returns:
Number -

字体大小。

Example
let fontSize = textSymbol.getFontSize();

getFontStyle() → {String}

获取字体类型eg:斜体。

Returns:
String -

字体的类型。

Example
let fontStyle = textSymbol.getFontStyle();

getFontWeight() → {String}

获取字体粗细。

Returns:
String -

字体的粗细。

Example
let fontWeight = textSymbol.getFontWeight();

getHaloColor() → {module:style~Color}

获取描边颜色。

Returns:
module:style~Color -

文字的描边颜色。

Example
let haloColor = textSymbol.getHaloColor();

getHaloWidth() → {Number}

获取描边宽度。

Returns:
Number -

文字描边的宽度。

Example
let haloWidth = textSymbol.getHaloWidth();

getLineRenderData(options, callback)

创建线文字图集,返回纹理坐标,

Parameters:
Name Type Description
options
callback

getLineRenderData2(options, callback)

临时使用 勿删

Parameters:
Name Type Description
options
callback

getOffset() → {module:basetype~Offset}

获取偏移量。

Returns:
module:basetype~Offset -

文字的偏移量。

Example
let offset = textSymbol.getOffset();

getOpacity() → {Number}

获取透明系数。

Returns:
Number -

透明系数。

Example
let opacity = textSymbol.getOpacity();

getOriginText() → {String}

获取原始文本内容。当原始文本太长时,会被截取展示。

Returns:
String -

原始文本内容。

Example
let text = textSymbol.getText();

getPaddingBottom() → {Number}

获取文字的底部内衬宽度

Returns:
Number -

文字的底部内衬宽度

getPaddingLeft() → {Number}

获取文字的左边内衬宽度

Returns:
Number -

文字的左边内衬宽度

getPaddingRight() → {Number}

获取文字的右边内衬宽度

Returns:
Number -

文字的右边内衬宽度

getPaddingTop() → {Number}

获取文字的顶部内衬宽度

Returns:
Number -

文字的顶部内衬宽度

getRotation() → {Number}

获取旋转角度。

Returns:
Number -

文字的旋转角度。

Example
let rotation = textSymbol.getRotation();

getRowTextCount() → {Number}

获取每行文字个数的最大值。

Returns:
Number -

每行文字个数的最大值。

Example
let rowTextCount = textSymbol.getRowTextCount();

getRowWidth() → {Number}

固定文字宽度的模式下,获取每行宽度。

Returns:
Number -

每行文字的宽度。

Example
let rowTextCount = textSymbol.getRowWidth();

getShadowColor() → {module:style~Color}

获取文字样式的阴影颜色

Returns:
module:style~Color -

文字样式的阴影颜色

getShadowOffset() → {Array.<Number>}

获取文字样式的阴影偏移量

Returns:
Array.<Number> -

文字样式的阴影偏移量

getText() → {String}

获取文本内容。

Returns:
String -

文本内容。

Example
let text = textSymbol.getText();

getTextAlign() → {String}

已废弃,建议使用getTextAlignment代替。用于获取文本水平排列方式。

Returns:
String -

'left'、'center'、'right'三者之一。

Example
let textAlign = textSymbol.getTextAlign();
Deprecated:
  • Yes

getTextAlignment() → {String}

获取文本水平排列方式。

Returns:
String -

'left'、'center'、'right'三者之一。

Example
let textAlignment = textSymbol.getTextAlignment();

getTextBaseline() → {String}

获取文本基线,即垂直排列的方式。

Returns:
String -

’top‘、’bottom‘、’middle‘三者之一。

Example
let textBaseline = textSymbol.getTextBaseline();

setBackgroundColor(color) → {module:style~Color}

设置背景填充颜色。

Parameters:
Name Type Description
color module:style~Color

背景填充色。

Returns:
module:style~Color -

修改后的背景填充色。

Example
let color = new hmap.style.Color(255,0,0,1);
let backgroundColor = textSymbol.setBackgroundColor(color);

setBackgroundImage(value)

设置文字样式背景图片

Parameters:
Name Type Description
value Image | String

图片,或者图片的url

setFontColor(color) → {module:style~Color}

设置字体的颜色。

Parameters:
Name Type Description
color module:style~Color

字的填充色。

Throws:

当参数不是Color时,抛出异常:"parameter 'color' must be instance of Color"。

Returns:
module:style~Color -

返回修改后的字的填充色。

Example
let color = new hmap.style.Color(255,0,0,1);
let result = textSymbol.setFontColor(color);

setFontFace(fontFace) → {String}

设置字体族名称。参数fontFace即css中的fontFamily,但是只支持单个字体族名称,不支持多个字体族组成的列表。
默认值是“Microsoft YaHei”,其他可取的值包括:"arial"、"serif"、"sans-serif"、"monospace"、"fantasy"、"cuisive"、"宋体"、"微软雅黑"、"华文细黑"、"黑体"等。

Parameters:
Name Type Description
fontFace String

字体族名称。

Throws:

当参数不是String类型时,抛出异常:"parameter 'fontFace' must be string"。

Returns:
String -

返回修改后的字体族名称。

Example
textSymbol.setFontFace("楷体");
let result = textSymbol.getFontFace();//result="楷体"

setFontSize(fontSize) → {Number}

设置字体大小。最小值为8,最大值为64.

Parameters:
Name Type Description
fontSize Number

字体大小,单位像素。默认是20,最大64。根据一般需求,建议定义13-64之间的整数,8-12会出现渲染模糊的问题。

Returns:
Number -

返回修改后的字体大小。

Example
let result = textSymbol.setFontSize(25);

setFontStyle(fontStyle) → {String}

设置字体样式。默认是“normal”,可取的值包括“normal”、“italic”、“oblique”。
1、normal 默认值。浏览器显示标准的字体样式。
2、italic 浏览器会显示一个斜体的字体样式。
3、oblique 浏览器会显示一个倾斜的字体样式。

Parameters:
Name Type Description
fontStyle String

字体样式。

Returns:
String -

字体样式。

Example
let fontStyle = textSymbol.setFontStyle("italic");

setFontWeight(fontWeight) → {String}

设置字体粗细,默认是"normal"。可以传入的值包括:"normal"、"bold"、"bolder",传入其它异常值,会导致不可预估的错误!

Parameters:
Name Type Description
fontWeight String

字体粗细。

Returns:
String -

返回修改后的字体粗细。

Example
textSymbol.setFontWeight("bold");
let result = textSymbol.getFontWeight();//result="bold"

setHaloColor(color) → {module:style~Color}

设置字的描边颜色。

Parameters:
Name Type Description
color module:style~Color

描边颜色。

Throws:
TypeError -

当参数不是Color时,抛出异常:"parameter 'color' must be instance of Color"。

Returns:
module:style~Color -

返回修改后的字的描边色。

Example
let color = new hmap.style.Color(255,1,1,1);
let haloColor = textSymbol.setHaloColor(color);

setHaloWidth(width) → {Number}

设置描边宽度。

Parameters:
Name Type Description
width Number

大于等于0。

Throws:

传入的参数小于0或者无穷大时,抛出异常:“parameter 'width' must be number”。

Returns:
Number -

描边的宽度。

Example
let haloWidth = textSymbol.setHaloWidth(3);

setOffset(offset) → {module:basetype~Offset}

设置文字的偏移量。

Parameters:
Name Type Description
offset module:basetype~Offset

偏移量。表示文字相对于坐标点的偏移量。

Throws:

当参数不是Offset时,抛出异常:"parameter 'offset' must be instance of Offset"。

Returns:
module:basetype~Offset -

返回修改后的偏移量。

Example
let offset = new hmap.basetype.Offset(1,1,0);
textSymbol.setOffset(offset);
let result = textSymbol.getOffset();//result=new hmap.basetype.Offset(1,1,0)

setOpacity(opacity) → {Number}

设置文字整体的透明系数。

Parameters:
Name Type Description
opacity Number

透明系数。

Returns:
Number -

返回透明系数。

Example
textSymbol.setOpacity(0.5);

setRotation(rotation) → {Number}

设置旋转角度。

Parameters:
Name Type Description
rotation Number

旋转角度。单位是弧度制。

Throws:

当参数是正、负无穷大时,抛出异常:"parameter 'rotation' must be number"。

Returns:
Number -

返回修改后的旋转角度。

Example
textSymbol.setRotation(Math.PI/4);
let result = textSymbol.getRotation();//result=Math.PI/4

setRowTextCount(rowTextCount) → {Number}

设置每行文字个数的上限。

Parameters:
Name Type Description
rowTextCount Number

文字个数的上限。

Returns:
Number -

修改后的每行文字个数的上限。

Example
let rowTextCount = textSymbol.setRowTextCount(rowTextCount);

setText(text) → {String}

设置文本内容。

Parameters:
Name Type Description
text String

文本内容。

Throws:

当参数类型不是String时,抛出异常:"parameter 'text' must be string"。

Returns:
String -

返回修改后的文本内容。

Example
textSymbol.setText("江陵路");
let result = textSymbol.getText();//result="江陵路"

setTextAlign(textAlign) → {String}

已废弃,建议使用setTextAlignment代替。用于设置文本水平排列方式,可选项包括'left','right','center'。

Parameters:
Name Type Description
textAlign String

文本水平排列方式。枚举值是:'left','right','center'。

Throws:

当参数不在上述枚举值范围之内时,抛出异常:"parameter 'textAlign' should be left, or right, or center"。

Returns:
String -

返回修改后的水平排列方式。

Example
textSymbol.setTextAlign("left");
let result = textSymbol.getTextAlign();//result="left"
Deprecated:
  • Yes

setTextAlignment(textAlignment) → {String}

设置文本水平排列方式。

Parameters:
Name Type Description
textAlignment String

文本水平排列方式。枚举值是:'left','right','center'。

Throws:

当参数不在上述枚举值范围之内时,抛出异常:"parameter 'textAlignment' should be left, or right, or center"。

Returns:
String -

返回修改后的水平排列方式。

Example
textSymbol.setTextAlignment("left");
let result = textSymbol.getTextAlignment();//result="left"

setTextBaseline(textBaseline) → {String}

设置文本基线。

Parameters:
Name Type Description
textBaseline String

垂直排列方式。枚举值是:"middle" "bottom" "top"。

Throws:

当参数不在上述枚举值范围内时,抛出异常:"parameter 'textBaseline' should be middle, or bottom, or top"。

Returns:
String -

返回修改后的垂直排列方式。

Example
textSymbol.setTextBaseline("bottom");
let result = textSymbol.getTextBaseline();//result="bottom"