富文本 richText 分段加载 ColorFont.lua
来源:互联网 发布:淘宝全网举报会怎么样 编辑:程序博客网 时间:2024/06/16 17:30
module("ColorFont",package.seeall)
local DEFAULT_FONT = "Droid Sans Fallback" -- "Droid Sans Fallback" "ttf/font.ttf" 在挂机的 config.lua 里
--和挂机不同,这里的颜色字采用分页
function create_label(text, fontSize) --注释的为挂机源码
-- if MY_LANGUAGE == LANGUAGE_TYPE.LANGUAGE_CH then
-- return cc.Label:createWithSystemFont( text, DEFAULT_FONT, size)
-- else
-- return cc.Label:createWithTTF( text, DEFAULT_FONT, size)
-- end
return cc.Label:createWithSystemFont( text, DEFAULT_FONT, fontSize)
end
function get_paragraphs(content)
return string.split(content, "[/br]")
end
-- iPar = last index of paragraphs, iRep = last index of replace, num = 加载多少段(number of paragraphs)
function get_rich_texts(paragraphs, replace, width, fontSize, iPar, iRep, num)
if iPar == #paragraphs then
return nil
end
local start = iPar + 1
local bound = math.min(#paragraphs, iPar + num)
iPar = bound
local node = cc.Node:create()
local nowHeight = 0
for i = start, bound do
local p = string.gsub(paragraphs[i], '#s#', function()
iRep = iRep + 1
if replace[iRep] ~= nil then
local replace_type = type(replace[iRep])
if replace_type == 'string' or replace_type == 'number' then
return replace[iRep]
else
return ''
end
end
return ''
end)
local richText, textSize = get_rich_label(paragraphs[i], width, fontSize )
nowHeight = nowHeight + textSize.height
richText:setAnchorPoint(cc.p(0.25, 0)) -- cc.p(0.25, ...) ??
richText:setPosition(cc.p(0, -nowHeight))
node:addChild(richText)
end
return node, nowHeight, iPar, iRep
end
function get_rich_label(_p, width, fontSize ) -- current paragraph
local richText = ccui.RichText:create()
richText:ignoreContentAdaptWithSize(false)
richText:setContentSize(cc.size(width, 0))
local fragments = format_paragraph(_p) -- 当前段产生的信息碎片集合
for i = 1, #fragments do
if fragments[i].color then
fontSize = math.floor(fontSize * fragments[i].color[2] * 1.0 / 100)
break
end
end
local lbl = create_label("", fontSize)
local str = ""
lbl:setDimensions(width, 0)
for i = 1, #fragments do
str = str .. fragments[i].text
end
lbl:setString(str)
local color = ''
local textSize = lbl:getContentSize()
for i = 1, #fragments do
if fragments[i].color then
color = GlobalVar[fragments[i].color[1]]
else
color = GlobalVar['white0']
end
local richElem = ccui.RichElementText:create(0, color, 255, fragments[i].text, DEFAULT_FONT, fontSize)
richText:pushBackElement(richElem)
end
if #fragments > 0 and fragments[1].text == "[/line]" then
richText:setVisible(false)
end
return richText,textSize
end
function format_paragraph(_p) -- current paragraph
local ret = {}
while true do
local start1, end1, color, fontSize = string.find(_p, '%[color=(%a+%d+),(%d+)%]')
if start1 == nil then
break
end
local start2, end2 = string.find(_p, "%[/color%]", end1 + 1)
local str1 = string.sub(_p, 1, start1 - 1)
local str2 = string.sub(_p, end1 + 1, start2 - 1)
if str1 ~= '' then
table.insert(ret, {text = str1})
end
if str2 ~= '' then
table.insert(ret, {text = str2, color = {color, fontSize}})
end
_p = string.sub(_p, end2 + 1)
end
if _p ~= '' then
table.insert(ret,{text = _p})
end
return ret
end
local DEFAULT_FONT = "Droid Sans Fallback" -- "Droid Sans Fallback" "ttf/font.ttf" 在挂机的 config.lua 里
--和挂机不同,这里的颜色字采用分页
function create_label(text, fontSize) --注释的为挂机源码
-- if MY_LANGUAGE == LANGUAGE_TYPE.LANGUAGE_CH then
-- return cc.Label:createWithSystemFont( text, DEFAULT_FONT, size)
-- else
-- return cc.Label:createWithTTF( text, DEFAULT_FONT, size)
-- end
return cc.Label:createWithSystemFont( text, DEFAULT_FONT, fontSize)
end
function get_paragraphs(content)
return string.split(content, "[/br]")
end
-- iPar = last index of paragraphs, iRep = last index of replace, num = 加载多少段(number of paragraphs)
function get_rich_texts(paragraphs, replace, width, fontSize, iPar, iRep, num)
if iPar == #paragraphs then
return nil
end
local start = iPar + 1
local bound = math.min(#paragraphs, iPar + num)
iPar = bound
local node = cc.Node:create()
local nowHeight = 0
for i = start, bound do
local p = string.gsub(paragraphs[i], '#s#', function()
iRep = iRep + 1
if replace[iRep] ~= nil then
local replace_type = type(replace[iRep])
if replace_type == 'string' or replace_type == 'number' then
return replace[iRep]
else
return ''
end
end
return ''
end)
local richText, textSize = get_rich_label(paragraphs[i], width, fontSize )
nowHeight = nowHeight + textSize.height
richText:setAnchorPoint(cc.p(0.25, 0)) -- cc.p(0.25, ...) ??
richText:setPosition(cc.p(0, -nowHeight))
node:addChild(richText)
end
return node, nowHeight, iPar, iRep
end
function get_rich_label(_p, width, fontSize ) -- current paragraph
local richText = ccui.RichText:create()
richText:ignoreContentAdaptWithSize(false)
richText:setContentSize(cc.size(width, 0))
local fragments = format_paragraph(_p) -- 当前段产生的信息碎片集合
for i = 1, #fragments do
if fragments[i].color then
fontSize = math.floor(fontSize * fragments[i].color[2] * 1.0 / 100)
break
end
end
local lbl = create_label("", fontSize)
local str = ""
lbl:setDimensions(width, 0)
for i = 1, #fragments do
str = str .. fragments[i].text
end
lbl:setString(str)
local color = ''
local textSize = lbl:getContentSize()
for i = 1, #fragments do
if fragments[i].color then
color = GlobalVar[fragments[i].color[1]]
else
color = GlobalVar['white0']
end
local richElem = ccui.RichElementText:create(0, color, 255, fragments[i].text, DEFAULT_FONT, fontSize)
richText:pushBackElement(richElem)
end
if #fragments > 0 and fragments[1].text == "[/line]" then
richText:setVisible(false)
end
return richText,textSize
end
function format_paragraph(_p) -- current paragraph
local ret = {}
while true do
local start1, end1, color, fontSize = string.find(_p, '%[color=(%a+%d+),(%d+)%]')
if start1 == nil then
break
end
local start2, end2 = string.find(_p, "%[/color%]", end1 + 1)
local str1 = string.sub(_p, 1, start1 - 1)
local str2 = string.sub(_p, end1 + 1, start2 - 1)
if str1 ~= '' then
table.insert(ret, {text = str1})
end
if str2 ~= '' then
table.insert(ret, {text = str2, color = {color, fontSize}})
end
_p = string.sub(_p, end2 + 1)
end
if _p ~= '' then
table.insert(ret,{text = _p})
end
return ret
end
0 0
- 富文本 richText 分段加载 ColorFont.lua
- PHPExcel处理富文本RichText
- cocos2dx --- 富文本的使用 RichText
- cocos2d-x改底层之RichText富文本换行
- 【Cocos2d-js教程】cocos2d-js 富文本 RichText
- Cocos2dx-- 聊天系统之富文本(RichText)与字体对齐
- cocos2dx中RichText富文本的换行问题
- webview 富文本加载
- Cocos+lua富文本控件
- webview的富文本加载
- iOS 富文本加载 HTML
- Unity 《知识技能碎片》Unity 富文本(RichText)的使用使UI效果更丰富
- iOS7加载富文本(UILabel)
- Android webview加载显示富文本
- Android TextView 富文本加载实现
- webview加载富文本混淆后出错
- iOS label加载html富文本内容
- 将lua中字符串转换成table,富文本
- 统计学习(一)--统计学习的定义及常识
- 安装redis
- Jersey 入门与Javabean
- Arc Hydro Tools Version 2.0 Are Now Available!
- 并发知识点札记
- 富文本 richText 分段加载 ColorFont.lua
- Windows 7环境 编译 mod_wsgi
- 全面探索 FreeMarker 模版引擎的扩展性
- php学习之 基础四 cookie和session
- 数据结构之链表的基本操作
- 三、创建一个可复用的静态单链表
- DIV+CSS 圆角边框
- HashBasedTable
- Android 导入v7包常见错误,以及项目引用v7包错误解决