search
HomeDatabaseMysql Tutorialcocos2dx实现功能强大的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 plaincopycocos2dx实现功能强大的RichText控件cocos2dx实现功能强大的RichText控件

  1. function ChatUI:initRichEdit()      
  2.     local widget = self:getWidget()  
  3.     if widget then  
  4.         --创建小喇叭控件  
  5.         self._richBugle = ui.RichTextUI:create()  
  6.         self._richBugle:setSize(cc.size(940, 35))  
  7.         self._richBugle:setAnchorPoint(cc.p(0, 0))  
  8.         self._richBugle:setPosition(cc.p(100, 510))  
  9.         self._richBugle:setMaxLine(1)  
  10.         --创建聊天控件  
  11.         self._richChat= ui.RichTextUI:create()  
  12.         self._richChat:setSize(cc.size(940, 420))  
  13.         self._richChat:setAnchorPoint(cc.p(0, 0))  
  14.         self._richChat:setPosition(cc.p(20, 70))    
  15.   
  16.         widget:addChild(self._richBugle)  
  17.         widget:addChild(self._richChat)  
  18.   
  19.         local function callback(sender, eventType)  
  20.             if eventType == ui.RICHTEXT_ANCHOR_CLICKED then  
  21.                 print(">>>>>>>>>>>addEventListenerRichText")  
  22.             end  
  23.         end  
  24.         self._richChat:addEventListenerRichText(callback)  
  25.     end   
  26. end  
  27.   
  28. function ChatUI:addChatMsg(channel, roleName, chatMsg, signs)  
  29.     local richText = (channel == Channel_ID_Bugle) and self._richBugle or self._richChat  
  30.     if richText and channel and roleName and chatMsg then  
  31.         local ChannelNameSwitch =   
  32.         {  
  33.             [Channel_ID_Team] = "【队伍】",  
  34.             [Channel_ID_Privacy] = "【私聊】",  
  35.             [Channel_ID_Faction] = "【帮会】",  
  36.             [Channel_ID_World] = "【世界】",  
  37.             [Channel_ID_System] = "【系统】"  
  38.         }  
  39.         local ChannelColor =   
  40.         {  
  41.             [Channel_ID_Team] = Color3B.ORANGE,  
  42.             [Channel_ID_Privacy] = Color3B.ORANGE,  
  43.             [Channel_ID_Faction] = Color3B.ORANGE,  
  44.             [Channel_ID_World] = Color3B.ORANGE,  
  45.             [Channel_ID_System] = Color3B.WHITE,  
  46.             [Channel_ID_Bugle] = Color3B.ORANGE  
  47.         }  
  48.         local linkColor = Color3B.YELLOW  
  49.         local linklineColor = Color4B.YELLOW     
  50.         local outlineColor = Color4B.BLACK    
  51.   
  52.         if channel == Channel_ID_Bugle then  
  53.             richText:insertNewLine()  
  54.         end  
  55.         if ChannelNameSwitch[channel] then  
  56.             local rc = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(ChannelNameSwitch[channel]), "DFYuanW7-GB2312.ttf", 25)      
  57.             rc:enableOutLine(outlineColor, 2)  
  58.             richText:insertElement(rc)  
  59.         end  
  60.         if channel ~= Channel_ID_System then  
  61.             local rcn = ui.RichItemText:create(channel, linkColor, 255, strg2u(roleName), "DFYuanW7-GB2312.ttf", 25)    
  62.             rcn:enableLinkLine(linklineColor, 1)  
  63.             rcn:enableOutLine(outlineColor, 2)  
  64.             richText:insertElement(rcn)  
  65.             chatMsg = ":" .. chatMsg  
  66.         end  
  67.         local rcm = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(chatMsg), "DFYuanW7-GB2312.ttf", 25)    
  68.         richText:insertElement(rcm)  
  69.         if channel ~= Channel_ID_Bugle then  
  70.             richText:insertNewLine()  
  71.         end  
  72.     end  
  73. end  
  74.   
  75. function ChatUI:initComponent()     
  76.     self:addChatMsg(Channel_ID_Bugle, "王小二", "This is Bugle Msg")  
  77.     self:addChatMsg(Channel_ID_System, "", "This is System Msg")  
  78.     self:addChatMsg(Channel_ID_Team, "王小二", "This is Team Msg")  
  79.     self:addChatMsg(Channel_ID_World, "王小二", "This is World Msg")  
  80.     self:addChatMsg(Channel_ID_Faction, "王小二", "This is Faction Msg")  
  81.   
  82.     self._channel = Channel_ID_World  
  83.     self:showChannel(Channel_ID_All)  
  84.     local btnChannel = self:getChild("Button_Channel")  
  85.     if btnChannel then  
  86.         btnChannel:setTitleText(strg2u("世界"))  
  87.     end      
  88. end  

        最后是效果图:

cocos2dx实现功能强大的RichText控件

         

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.