cocos2dx实现功能强大的RichText控件
最近准备做一个聊天系统,开始准备使用cocos2dx的UIRichText控件来显示聊天内容,结果在使用的时候才发现,cocos2dx的RichText功能非常有限,完全不具备实现聊天的功能,只实现了加入文本、图像和自定义控件的功能,支持不同字体、颜色、字号。 我个人认为,
最近准备做一个聊天系统,开始准备使用cocos2dx的UIRichText控件来显示聊天内容,结果在使用的时候才发现,cocos2dx的RichText功能非常有限,完全不具备实现聊天的功能,只实现了加入文本、图像和自定义控件的功能,支持不同字体、颜色、字号。
我个人认为,一个RichText控件应该具备以下基本功能:
1、多样化的文本显示功能,包括字体、颜色、字号的设置。
2、能显示图片以及一些特殊元素。
3、应该支持图片文字的超链接功能。
4、能够支持滚动的效果。
5、能够有很方便的换行功能,最好能设置行间距。
如果能够更好的实现聊天的功能,我觉得还需要加入以下功能:
1、文本特效:描边,下划线,阴影,发光等功能。
2、支持设置控件最大显示行数。
3、支持数据的分类显示,用于分频道显示聊天内容。
cocos2dx只实现了基础的1和2功能,所以考虑之后还是决定自己写一个RichText控件。UIRichText的框架还是不错的,实现了文本分行显示的技术。在他的基础上很容易扩展。
首先,扩展RichItem,用来支持多样化的文本需求。
其次,扩展Label控件,用于支持特殊的文字效果。
再次,需要实现滚动功能,控件继承UIScrollView。
最后,还需要对lua进行支持,包括使用功能以及超链接点击事件的注册。
以上是我实现控件的思路,这里就不贴代码了,很多,我会把我的控件代码共享给大家,大家在使用中有什么问题也可以向我咨询。
源代码在这里,cocos2dx-3.0功能强大的richText控件
最后贴一下使用的代码和效果图吧!
使用代码如下,我是在lua里面使用的,大家可以参考一下:
[plain]
view plaincopy
- function ChatUI:initRichEdit()
- local widget = self:getWidget()
- if widget then
- --创建小喇叭控件
- self._richBugle = ui.RichTextUI:create()
- self._richBugle:setSize(cc.size(940, 35))
- self._richBugle:setAnchorPoint(cc.p(0, 0))
- self._richBugle:setPosition(cc.p(100, 510))
- self._richBugle:setMaxLine(1)
- --创建聊天控件
- self._richChat= ui.RichTextUI:create()
- self._richChat:setSize(cc.size(940, 420))
- self._richChat:setAnchorPoint(cc.p(0, 0))
- self._richChat:setPosition(cc.p(20, 70))
- widget:addChild(self._richBugle)
- widget:addChild(self._richChat)
- local function callback(sender, eventType)
- if eventType == ui.RICHTEXT_ANCHOR_CLICKED then
- print(">>>>>>>>>>>addEventListenerRichText")
- end
- end
- self._richChat:addEventListenerRichText(callback)
- end
- end
- function ChatUI:addChatMsg(channel, roleName, chatMsg, signs)
- local richText = (channel == Channel_ID_Bugle) and self._richBugle or self._richChat
- if richText and channel and roleName and chatMsg then
- local ChannelNameSwitch =
- {
- [Channel_ID_Team] = "【队伍】",
- [Channel_ID_Privacy] = "【私聊】",
- [Channel_ID_Faction] = "【帮会】",
- [Channel_ID_World] = "【世界】",
- [Channel_ID_System] = "【系统】"
- }
- local ChannelColor =
- {
- [Channel_ID_Team] = Color3B.ORANGE,
- [Channel_ID_Privacy] = Color3B.ORANGE,
- [Channel_ID_Faction] = Color3B.ORANGE,
- [Channel_ID_World] = Color3B.ORANGE,
- [Channel_ID_System] = Color3B.WHITE,
- [Channel_ID_Bugle] = Color3B.ORANGE
- }
- local linkColor = Color3B.YELLOW
- local linklineColor = Color4B.YELLOW
- local outlineColor = Color4B.BLACK
- if channel == Channel_ID_Bugle then
- richText:insertNewLine()
- end
- if ChannelNameSwitch[channel] then
- local rc = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(ChannelNameSwitch[channel]), "DFYuanW7-GB2312.ttf", 25)
- rc:enableOutLine(outlineColor, 2)
- richText:insertElement(rc)
- end
- if channel ~= Channel_ID_System then
- local rcn = ui.RichItemText:create(channel, linkColor, 255, strg2u(roleName), "DFYuanW7-GB2312.ttf", 25)
- rcn:enableLinkLine(linklineColor, 1)
- rcn:enableOutLine(outlineColor, 2)
- richText:insertElement(rcn)
- chatMsg = ":" .. chatMsg
- end
- local rcm = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(chatMsg), "DFYuanW7-GB2312.ttf", 25)
- richText:insertElement(rcm)
- if channel ~= Channel_ID_Bugle then
- richText:insertNewLine()
- end
- end
- end
- function ChatUI:initComponent()
- self:addChatMsg(Channel_ID_Bugle, "王小二", "This is Bugle Msg")
- self:addChatMsg(Channel_ID_System, "", "This is System Msg")
- self:addChatMsg(Channel_ID_Team, "王小二", "This is Team Msg")
- self:addChatMsg(Channel_ID_World, "王小二", "This is World Msg")
- self:addChatMsg(Channel_ID_Faction, "王小二", "This is Faction Msg")
- self._channel = Channel_ID_World
- self:showChannel(Channel_ID_All)
- local btnChannel = self:getChild("Button_Channel")
- if btnChannel then
- btnChannel:setTitleText(strg2u("世界"))
- end
- end
最后是效果图:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
