Repository navigation
Expand file tree
/
Copy pathHtmlLabel.cpp
More file actions
132 lines (113 loc) · 2.9 KB
/
Copy pathHtmlLabel.cpp
File metadata and controls
132 lines (113 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "HtmlLabel.h"
#include "utils/LangUtil.h"
using namespace std;
HtmlLabel::HtmlLabel(CCSize labSize,string strHtmlText)
{
m_strHtmlText = strHtmlText;
m_labSize = labSize;
m_nElementTag = 1;
}
HtmlLabel::~HtmlLabel()
{
}
HtmlLabel* HtmlLabel::create(CCSize labSize,string strHtmlText)
{
HtmlLabel *pHtmlLabel = new HtmlLabel(labSize,strHtmlText);
pHtmlLabel->init();
pHtmlLabel->autorelease();
pHtmlLabel->initUI();
return pHtmlLabel;
}
void HtmlLabel::onEnter()
{
UILayout::onEnter();
}
void HtmlLabel::onExit()
{
UILayout::onExit();
}
void HtmlLabel::initUI()
{
m_htmlText = HtmlRichText::create();
m_htmlText->ignoreContentAdaptWithSize(false);
m_htmlText->setSize(m_labSize);
this->setSize(m_labSize);
parseHtmlText(m_strHtmlText);
addChild(m_htmlText);
}
void HtmlLabel::parseHtmlText(string strHtmlText)
{
using namespace liigo;
HtmlParser htmlParser;
htmlParser.parseHtml(strHtmlText.c_str());
for(int index = 0, count = htmlParser.getHtmlNodeCount(); index < count; index++)
{
HtmlNode* pNode = htmlParser.getHtmlNode(index);
if (pNode->type == NODE_START_TAG)
{
switch (pNode->tagType)
{
case TAG_FONT:
{
ccColor3B contentColor = ccBLACK;
int nSize = 20;
if(pNode->attributeCount>0)
{
string strColor=HtmlParser::getAttributeStringValue(pNode,"color","#000000");
string strSize = HtmlParser::getAttributeStringValue(pNode,"size","20");
if(strColor!="")
{
contentColor = getColor(strColor);
}
if (strSize!= "")
{
nSize = atoi(strSize.c_str());
}
}
index++;
liigo::HtmlNode* nextNode = htmlParser.getHtmlNode(index);
if(nextNode->type==NODE_CONTENT)
{
std::string content=nextNode->text;
HtmlRichElementText* htElem = HtmlRichElementText::create(m_nElementTag, contentColor, 255,content.c_str(), "Helvetica", nSize);
m_htmlText->pushBackElement(htElem);
m_nElementTag++;
}
break;
}
case TAG_BR:
{
HtmlRichElementAddLine* htElem = HtmlRichElementAddLine::create();
m_htmlText->pushBackElement(htElem);
break;
}
case TAG_A:
{
break;
}
default:
break;
}
}
if (pNode->type == NODE_CONTENT)
{
ccColor3B contentColor = ccBLACK;
int nSize = 20;
std::string content=pNode->text;
HtmlRichElementText* htElem = HtmlRichElementText::create(m_nElementTag, contentColor, 255,content.c_str(), "Helvetica", nSize);
m_htmlText->pushBackElement(htElem);
m_nElementTag++;
}
if (pNode->type == NODE_END_TAG)
{
}
}
}
ccColor3B HtmlLabel::getColor(std::string str)
{
int r,g,b;
sscanf(str.substr(1,2).c_str(),"%x",&r);
sscanf(str.substr(3,2).c_str(),"%x",&g);
sscanf(str.substr(5,2).c_str(),"%x",&b);
return ccc3(r,g,b);
}