_Font

字体类,功能包括字体的创建、设置,以及在该字体下字符串的相关操作。 全部展开

静态属性展开

  • + hCenter : uint

    水平居中。

  • + hLeft : uint

    水平左对齐。

  • + hRight : uint

    水平右对齐。

  • + vBottom : uint

    垂直下对齐。

  • + vCenter : uint

    垂直居中。

  • + vTop : uint

    垂直上对齐。

属性展开

  • + edgeColor : uint

    字体描边颜色

    • 示例

       font.edgeColor = _Color.Black
  • + glowColor : uint

    字体发光颜色

    • 示例

       font.glowColor = _Color.White
  • + lineSpace : uint

    行间距。

    • 示例

       font.lineSpace = 3
  • + offsetH : uint

    描边位置的的水平方向偏移,可造成字体阴影的效果。

    • 示例

       font.offsetH = 1
  • + offsetV : uint

    描边位置的的垂直方向偏移,可造成字体阴影的效果。

    • 示例

       font.offsetV = 1
  • + textColor : uint

    文本颜色

    • 示例

       font.textColor = _Color.Black

只读属性展开

  • + blur : bool

    模糊效果。

    • 示例

       print(font.blur)
  • + bold : bool

    加粗。

    • 示例

       print(font.bold)
  • + edge : uint

    字体边缘大小。

    • 示例

       print(font.edge)
  • + glow : uint

    发光值。

    • 示例

       print(font.glow)
  • + height : uint

    字体高度。

    • 示例

       print(font.height)
  • + italic : bool

    斜体。

    • 示例

       print(font.italic)
  • + resname : string

    资源文件名。

    • 示例

       print(font.resname)
  • + sharpen : bool

    锐化效果。

    • 示例

       print(font.sharpen)
  • + size : uint

    字号。

    • 示例

       print(font.size)
  • + strikeout : bool

    删除线。

    • 示例

       print(font.strikeout)
  • + underline : bool

    下划线。

    • 示例

       print(font.underline)

构造方法展开

  • + function _Font.new(fontName : string, size : uint[, glow : uint][, edge : uint][, bold : bool][, italic : bool][, underline : bool][, strikeout : bool][, sharpen : bool][, blur : bool])

    构造一种字体。

    • 参数
    • fontName : 字体名。
    • size : 字体大小。
    • glow : 发光宽度。默认值为0。
    • edge : 描边宽度。默认值为0。
    • bold : 是否加粗。默认值为false。
    • italic : 是否斜体。默认值为false。
    • underline : 是否添加下划线。默认值为false。
    • strikeout : 是否添加删除线。默认值为false。
    • sharpen : 是否有锐化效果。
    • blur : 是否有模糊效果。

    • 示例

       font = _Font.new('ARIAL', 19, 0, 0)

公共方法展开

  • + function clone(font : _Font) : _Font

    依照参数中的字体创建一个新的字体。

    • 参数
    • font : _Font的一个对象。
    • 返回
    • 新的字体对象。

    • 示例

       font2 = font:clone(font1)
  • + function cutLines(text : string, maxlength : uint, leadspace : uint) : string []

    在当前字体,指定行宽下,将字符串按自动换行方式,分割为多行。

    • 参数
    • text : 被分割的字符串。
    • maxlength : 每行行宽。
    • leadspace : 第一行开头空的像素,供排版用。
    • 返回
    • string数组。数组每个元素为分割后每行的字符串。
    • 示例

       strChi = '青果灵动是国内第一家自主研发3D网页游戏的技术力量型公司,我们执着于做好玩的游戏,我们是3D网页游戏领域中的先行者。'
      strEng = 'Fancyguo is the first independent researching and developing 3D technology-based webpage game company, we are dedicated to developing the best game.'
      str = strEng .. strChi
      strings = font:cutLines(str, 100, 40)
  • + function draw3DText(x : number, y : number, z : float, s : number, text : string)

    在3d世界中渲染字符串。

    • 参数
    • x : x坐标。
    • y : y坐标。
    • z : z坐标。
    • s : 高度值。
    • text : 字符串。
    • 示例

       font:draw3DText(0, 0, 0, 10, 'Hello World')
  • + function drawText(l : int, t : int, r : int, b: int, text : string[, align : uint])

    渲染字符串。

    • 参数
    • l : 渲染区域的左坐标。
    • t : 渲染区域的上坐标。
    • r : 渲染区域的右坐标。
    • b : 渲染区域的下坐标。
    • text : 字符串。
    • align : 对齐方式,可选参数。默认为左上对齐,其他对齐类型有:居右hRight、水平居中hCenter、居下vBottom、垂直居中vCenter。可用运算符"+"复合使用。
    • 示例

       font:drawText(0, 56, 'Press 2 to switch underline', _Font.hCenter + _Font.vCenter)
  • + function stringHeight(text : string) : int

    获取字符串的高度。

    • 参数
    • text : 字符串。
    • 返回
    • 字符串高度。类型为int。
    • 示例

       stringLen = font:stringHeight(str)
  • + function stringSize(text : string) : Point

    获取字符串的宽度和高度。

    • 参数
    • text : 字符串。
    • 返回
    • Point类型,其属性x为字符串宽度,属性y为字符串高度。
    • 示例

       stringS = font:stringSize(str)
  • + function stringWidth(text : string) : int

    获取字符串的宽度。

    • 参数
    • text : 字符串。
    • 返回
    • 字符串宽度。类型为int。
    • 示例

       stringLen = font:stringWidth(str)

代码示例

    str = 'hello fancyguo 你好'
    size = 19
    glow = 0
    edge = 0
    alpha = 0xff

    font = _Font.new('ARIAL', size, glow, edge)
    textcolor,growcolor,edgecolor = _Color.White,_Color.Red,_Color.Orange

    function newfont()
    font = _Font.new('ARIAL', size, glow, edge)
    font.textColor = textcolor
    font.growColor = growcolor
    font.edgeColor = edgecolor
    end

    function newcolor()
    textcolor = _or(_and(0x00ffffff, textcolor), _and(0xff000000, alpha* 0x1000000))
    edgeColor = _or(_and(0x00ffffff, edgeColor), _and(0xff000000, alpha* 0x1000000))
    font.textColor = textcolor
    font.edgeColor = edgecolor
    end

    newfont()

    _app:onKeyDown(function(key)
    if key == _System.KeyQ then
    if(size < 50) then size = size + 1 newfont() end
    elseif key == _System.KeyA then
    if(size > 2) then size = size - 1 newfont() end
    elseif key == _System.KeyW then
    if(glow < 5) then glow = glow + 1 newfont() end
    elseif key == _System.KeyS then
    if(glow > 0) then glow = glow - 1 newfont() end
    elseif key == _System.KeyE then
    if(edge < 5) then edge = edge + 1 newfont() end
    elseif key == _System.KeyD then
    if(edge > 0) then edge = edge - 1 newfont() end
    elseif key == _System.KeyR then
    if(alpha < 255) then alpha = alpha + 5 newcolor() end
    elseif key == _System.KeyF then
    if(alpha > 0) then alpha = alpha - 5 newcolor() end
    end
    end)

    noticefont = _Font.new('Arial', 10)
    noticefont.textColor = _Color.Yellow

    _app:onIdle(function(e)
    noticefont:drawText(0, 0, 'Press QA to adjust size')
    noticefont:drawText(0, 14, 'Press WS to adjust glow')
    noticefont:drawText(0, 28, 'Press ED to adjust edge')
    noticefont:drawText(0, 42, 'Press RF to adjust alpha')

    _rd:drawRect(_rd.w / 2 - 350, _rd.h / 2 - 150, _rd.w / 2, _rd.h / 2, 0xffffffff)
    font:drawText(_rd.w / 2 - 350, _rd.h / 2 - 150, _rd.w / 2, _rd.h / 2, str, _Font.hCenter + _Font.vCenter)
    end)
问题反馈(登录才可以发表哦!)