cocos2d-lua 3.4版本的富文本[转载的]

-- Author: zlf
-- Date: 2016年5月9日17:05:37
-- 简易富文本

RichText = class("RichText", function()
    return ccui.Layout:create()
end)

--自适应最大宽度,自动换行
RichText.auto = 1
--系统公告,无限宽
RichText.gmnotice = 2

local cache = cc.SpriteFrameCache:getInstance()

--[[
    @param data 富文本参数(table)
    data示例:{{txt="12呵呵asdasad###3", color="#FF9966", isUnLine = 1, data = "函数回调参数"}, {img = "Star.png", data = "函数回调参数"}}

    使用方式:
    local richLabel = RichText:create()
    setDrawType可用可不用,但是create之后一定要手动调用init函数
    richLabel:setDrawType(1)
    richLabel:init(param, 300)
    richLabel:setPosition(300, 320)
    self:addChild(richLabel) 

    data里面元素作用详解:
    txt代表要创建的控件是ccui.Text,img则代表ccui.ImageView, color设置ccui.Text的字体颜色
    isUnLine用来表示ccui.Text是否需要下划线,nil或者false代表不画, data作为控件回调函数的参数
    fontSize用来设置ccui.Text的字体大小

    @param maxWidth 富文本的最大宽度,设置了最大宽度,可以自动换行

    @param globalCallBack 全局回调函数,当这个参数存在时,所有的控件回调都调用这个函数

    @param globalFontSize 全局字体大小,当这个参数存在时,所有的ccui.Text的字体大小都设置为这个参数

    @param this 类指针,callback(this, callParam)这个作用,语法糖

]]
function RichText:create()
    local ret = RichText.new()
    return ret
end

--2016年6月12日19:53:03 去掉了几个全局参数  callback字段也不用了
-- function RichText:init(param, maxWidth, globalCallBack, globalColor, globalFontSize, this)
function RichText:init(param, maxWidth, globalCallBack, this)
    if type(param) ~= "table" then
        return
    end
    self:removeAllChildren()
    self.globalCallBack = globalCallBack
    self.globalFontSize = globalFontSize
    self.renderWidth = 0
    self.renderLine = 0
    maxWidth = maxWidth or 400
    local curWidth = 0
    local row = 0
    local curIndex = 1
    local allWidget = {}
    for i=1,#param do
        local x, y = 0
        if param[i].txt and param[i].txt ~= "" then
            local textTb = Utils.separate(param[i].txt)
            for j=1,#textTb do
                local color = self:getNeedColor(globalColor, param[i].color)
                local widget = self:getLabel(textTb[j], color, globalCallBack, param[i].data, globalFontSize or param[i].fontSize, this)
                widget.isUnLine = param[i].isUnLine
                local size = widget:getContentSize()
                x, y = curWidth, size.height * row
                local width = size.width
                if curWidth + width > maxWidth then
                    curWidth = width
                    row = row - 1
                    curIndex = curIndex + 1
                    x, y = 0, size.height * row
                else
                    curWidth = curWidth + width
                end
                if not allWidget[curIndex] then
                    allWidget[curIndex] = {}
                end
                table.insert(allWidget[curIndex], widget)
                widget:setPosition(x, y)
                widget:setAnchorPoint(cc.p(0, 0.5))
                widget.trueRow = curIndex
                self:addChild(widget)
            end
        elseif param[i].img then
            local widget = self:getImage(param[i].img, globalCallBack, globalParam or param[i].data, this)
            local size = widget:getContentSize()
            x, y = curWidth, size.height * row
            if curWidth + size.width > maxWidth then
                curWidth = size.width
                row = row - 1
                curIndex = curIndex + 1
                x, y = 0, size.height * row
            else
                curWidth = curWidth + size.width
            end
            if not allWidget[curIndex] then
                allWidget[curIndex] = {}
            end
            table.insert(allWidget[curIndex], widget)
            widget:setAnchorPoint(cc.p(0, 0.5))
            widget:setPosition(x, y)
            self:addChild(widget)
        elseif param[i].anim then
        end
    end
    self.renderWidth = curIndex > 1 and maxWidth or curWidth
    self.renderLine = curIndex
    self:adjust(allWidget)
end

function RichText:setDrawType(type)
    self.DrawType = type
end

function RichText:setRowSpace(space)
    self.rowSpace = space
end

function RichText:adjust(allWidget)
    self.DrawType = self.DrawType or self.auto
    self.renderHeight = 0
    if self.DrawType == self.auto then
        self.rowSpace = self.rowSpace or 0
        local _max = {}
        for i=1,#allWidget do
            local maxHeight = 0
            for k,v in pairs(allWidget[i]) do
                local size
                if v.isAnim then
                    size = v.size
                else
                    size = v:getContentSize()
                end
                if size.height * 0.5 > maxHeight then
                    maxHeight = size.height * 0.5 + self.rowSpace
                end
            end
            self.renderHeight = self.renderHeight + maxHeight
            if i == 1 then
                self.posX = maxHeight - self.rowSpace
            end
            if maxHeight > 0 then
                if _max[i-1] then
                    _max[i] = maxHeight*2 + _max[i-1]
                else
                    _max[i] = maxHeight
                end

                for k,v in pairs(allWidget[i]) do
                    local height = 0
                    if _max[i-1] then
                        height = -maxHeight - _max[i-1]
                    else
                        height = 0
                    end
                    v:setPositionY(height)
                    if v.isUnLine then
                        self:drawUnLine(self, v)
                    end
                end
            end
        end
        self:setContentSize(cc.size(self.renderWidth, self.renderHeight))
    else
        local all = {}
        local allWidth = {}
        local maxHeight = -100
        for k,v in pairs(allWidget) do
            for key,value in pairs(v) do
                local size
                if value.isAnim then
                    size = value.size
                else
                    size = value:getContentSize()
                end
                if size.height * 0.5 > maxHeight then
                    maxHeight = size.height * 0.5
                end
                table.insert(all, value)
                table.insert(allWidth, value:getContentSize().width)
            end
            if k == 1 then
                self.posX = maxHeight
            end
        end
        self.renderHeight = maxHeight
        local curWidth = 0
        for i=1,#all do
            all[i]:setPosition(curWidth, 0)
            curWidth = curWidth + allWidth[i]
            self.renderWidth = curWidth
            if all[i].isUnLine then
                self:drawUnLine(self, all[i])
            end
        end
        self.renderLine = 1
    end
    self:setContentSize(cc.size(self.renderWidth, 0))
end

function RichText:getRenderWidth()
    return self.renderWidth
end

function RichText:getRenderLine()
    return self.renderLine
end

function RichText:getRenderHeight()
    return self.renderHeight
end

function RichText:drawUnLine(parent, widget)
    local color4F = cc.convertColor(widget:getColor(), "4f")
    color4F.a = 1
    local pos = cc.p(widget:getPosition())
    self:getDrawNode(parent):drawLine(cc.p(pos.x, pos.y - widget:getContentSize().height/2), cc.p(pos.x + widget:getContentSize().width, pos.y - widget:getContentSize().height/2), color4F)
end

function RichText:getDrawNode(parent)
    if not self.drawNode then
        self.drawNode = cc.DrawNode:create()
        parent:addChild(self.drawNode)
    end
    return self.drawNode
end

function RichText:getLabel(param, color, callback, funcParam, fontSize, this)
    local text = ccui.Text:create()
    text:setFontName("Arial")
    text:setFontSize(fontSize or 22)
    text:setString(param)
    if color then
        text:setColor(color)
    end
    if funcParam then
        text:setTouchEnabled(true)
        if type(callback) == "function" then
            text:addTouchEventListener(function(sender, event)
                if event == ccui.TouchEventType.ended then
                    if this then
                        callback(this, funcParam)
                    else
                        callback(funcParam)
                    end
                end
            end)
        end
    end
    text:enableShadow(cc.c4b(0,0,0,255), cc.size(1,-1))
    return text
end

function RichText:getImage(param, callback, callParam, this)
    local img = ccui.ImageView:create()
    if not cache:getSpriteFrame(param) then
        img:loadTexture(param)
    else
        img:loadTexture(param, ccui.TextureResType.plistType)
    end
    if callParam then
        img:setTouchEnabled(true)
        if type(callback) == "function" then
            img:addTouchEventListener(function(sender, event)
                if event == ccui.TouchEventType.ended then
                    if this then
                        callback(this, callParam)
                    else
                        callback(callParam)
                    end
                end
            end)
        end
    end
    return img
end

function RichText:getNeedColor(p1, p2)
    local result
    if p1 then
        result = p1
    elseif p2 then
        result = p2
    else
        result = cc.c3b(255,255,255)
    end
    if type(result) == "string" then
        result = Utils.str2Color(result)
    end
    return result
end

function RichText:setPos(x, y)
    self.posX = self.posX or 0
    if type(x) == "table" then
        x.y = x.y - self.posX
        self:setPosition(x)
    else
        y = y - self.posX
        self:setPosition(x, y)
    end
end
时间: 2024-08-25 11:42:15

cocos2d-lua 3.4版本的富文本[转载的]的相关文章

获取当前游览器名称以及版本 以及富文本编辑器 时候 光标高度问题

function getBrowserInfo() { var agent = navigator.userAgent.toLowerCase(); var regStr_ie = /msie [\d.]+;/gi; var regStr_ff = /firefox\/[\d.]+/gi var regStr_chrome = /chrome\/[\d.]+/gi; var regStr_saf = /safari\/[\d.]+/gi; //IE if (agent.indexOf("msie

在MVC中应用百度富文本编辑器

1.下载.NET版本的百度富文本编辑器,前往 下载.NET版本百度富文本框 2.解压下载的.zip压缩包,将utf8-.net文件夹名称改为:ueditor,复制到MVC根目录下面.结构如下: App_Code 上的文件是应用程序的源码 Config.cs 负责读取配置文件 Handler.cs 是请求处理器的基类,提供了一些基本对象的访问以及输出控制.如果需要增加处理器,应该从该基类继承 PathFormatter.cs 解析 PathFormat,把信息填充为运行时信息. *Handler.

[寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件

RichTextEx一款通过HTML标签控制文字样式的富文本控件 原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 下载地址 Github链接 这个是干什么的 将例如以下文字内容 "<#F37C2A><font Helvetica><30>[世]<#3AB5B3><underLine true>寒江孤叶<underLine

cocos2d-x中,简单html富文本显示

作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037414.html  虽然自从cocos2d-x更新到3.0后,使用freetype,并且增加了丰富文本,但这些文本都需要自己去设置,用起来也不方便,所以动手写了个简单html富文本 可以使用 <size=15></size>//字体大小 <fontname=“Arial”></fontname>//字体,这里必须有这个字体才能使用 <ou

TinyMCE(富文本编辑器)

[转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 官网演示以及示例代码:https://www.tinymce.com/docs/demo/image-tools/ 转自:http://www.cnblogs.com/hahacjh/archive/2010/07/24/1784268.html TinyMCE 在Asp.Net中的使用方法其实挺简单的,从官方网站下载TinyMCE),然后将里面的jscripts目录拷到你的网站目录 假设你的aspx页面中某一个地方需要用到编辑器

【微信公众平台开发】发布动态新闻好帮手UEditor富文本

由于微信要做发布动态新闻,那就需要富文本.上网搜索有很多这种插件,比如CKEditor,KindEditor等:最后看到百度一款开源的UEditor,官网打开,风格设计就吸引住了自己.所以就选UEditor了. 第一步:下载源码:由于是用php开发,所以下载版本是php版的,为了兼容以前的IE版本,选择版本1.4.2.UEditor 第二步:把下载下来的ueditor1_4_2-utf8-php.zip解压,并复制ueditor1_4_2-utf8-php文件到appache服务器底下(比如我的

实现一个简易的富文本编辑器(二):给富文本添加自定义事件

为富文本添加一个提交按钮,点击按钮可以获取富文本内容.但是在提交前或者提交后我想做一些操作,比如内容校验内容清空等等. 我们直接在该按钮上绑定点击事件同样可以达到目的,但是为了组件化,所以本例打算为提交按钮自定义beforeSubmit.afterSubmit两个事件. 1.创建发布订阅者对象 前文说到,事件系统是发布-订阅模式的一个实现,模式给事件发布函数与事件处理函数进行解耦,使得两者无直接调用关系. 简易发布订阅者对象实现如下: var Event = { // _cachePool :

MVC富文本编辑器CKEditor配置CKFinder

富文本编辑器CKEditor的使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin:

Django集成百度富文本编辑器uEditor

UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码. 首先从ueEditor官网下载最新版本的包,目前官网上提供了ASP..NET.PHP.JSP版本的,django版本只有一个第三方个人开发的,但看上出配置起来稍微复杂一点. 这里不介绍uEditor的使用方法,也不过多解释uEditor的配置方法,官网上都有详细的文档和API介绍,下载的Demo中也有常用的方法的示例代码,这里主要介绍uEdi