Cocos2d-x计算字符串Size方法

项目需要,根据输入字符串,计算字符串所需要占的Size。封装代码如下,只需传入字符串,即可返回Size:

Size ChartDemoScene::calculateFontSize(const char *str )
{
	std::string tempString = str;
	log("tempString = %s",tempString.c_str());
	size_t computeCount = tempString.size();       //如果字符串很长每次抽取100个字符来计算size;
	Size size = Size(0,0);
	for (int i = 0; i<computeCount ;)
	{
        std::string substring =  tempString.substr(i,1);
		if ((substring.c_str()[0] & 0x80 )!=0) //是汉字
		{
			substring = tempString.substr(i , 3);
			i += 3;
		}
		else
		{
			i++;
		}
		//CCLog("subString  = %s ",substring.c_str());
        auto tempLabel = LabelTTF::create(substring.c_str(),"",25);
        tempLabel->setHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
		Size tmpLsize = tempLabel->getContentSize();
		size.width+=tmpLsize.width;
	}

	float fHeight= 0;
	if( size.width > chartWidth)//大于容器的宽度
	{
        fHeight = (size.width / 200 );//计算需要多少行
	}
	int nHeight =  ceil(fHeight);

	if (nHeight == 0)
	{
		nHeight = 1;
	}

	Size labelSize ;
	if (size.width < chartWidth)
	{
		labelSize = Size(size.width,nHeight*32);//计算容器的Size
	}
	else
	{
        labelSize = Size(chartWidth,nHeight*28);
	}

	//CCLog("labelSize = (%f, %f)",labelSize.width ,labelSize.height);
	//CCLog("fHeight = %f  nHeight = %d " ,fHeight ,nHeight);
	return labelSize;
}

例子:

1、AppDelegate.cpp中添加如下代码

auto scene = ChartDemoScene::createScene();

    // run
    director->runWithScene(scene);

(1)ChartDemoScene.hChartDemoScene.h

//
//  ChartDemoScene.h
//  chartDemo
//
//  Created by chen on 14-9-2.
//
//

#ifndef __chartDemo__ChartDemoScene__
#define __chartDemo__ChartDemoScene__

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "../cocos2d/extensions/cocos-ext.h"
using namespace cocos2d::ui;
USING_NS_CC;
USING_NS_CC_EXT;

class ChartDemoScene : public cocos2d::Layer//,public cocos2d::extension::EditBoxDelegate
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(ChartDemoScene);

//    virtual void editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox);
//    virtual void editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox);
//    virtual void editBoxTextChanged(cocos2d::extension::EditBox* editBox, const std::string& text);
//    virtual void editBoxReturn(cocos2d::extension::EditBox* editBox);

    void touchEvent(Ref *pSender, Widget::TouchEventType type);
    void textFieldEvent(Ref* pSender, TextField::EventType type);
    void showAlertShow(std::string contentString);
    void alertCallback(ui::Text* alert);

    Size calculateFontSize(const char *str);

    std::string getcurrTime();//获取当前年月日
    std::string getcurrMonthTime();//获取时分秒

//    EditBox* editText;
    TextField* editText;
    Text* text;
    TextField* _text;//获取被删除Item的内容
    std::string file_path;//存放消息记录
    TextField* textContent;
    ui::Button* sendBtn;
    std::string str;
    Size winSize;
    float chartWidth;
    size_t length;
    ui::ListView* _listView;

};
#endif /* defined(__chartDemo__ChartDemoScene__) */

(2)ChartDemoScene.cpp

#include "ChartDemoScene.h"
#include "stdio.h"

enum alertTag
{
    Alert_Tag = 1000

}AlertTag;

enum textTag
{
    T_Tag = 100
}TextTag;

USING_NS_CC;
int count = 1;
int _rem = 0;
Scene* ChartDemoScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    scene->setColor(Color3B::BLACK);
    // 'layer' is an autorelease object
    auto layer = ChartDemoScene::create();
    layer->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
    layer->setContentSize(Size(640,960));
    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool ChartDemoScene::init()
{
    bool bRet = false;
    do{
        CC_BREAK_IF(!Layer::init());
        //大背景
        winSize = Director::getInstance()->getWinSize();
        file_path = FileUtils::getInstance()->getWritablePath() + "chartContent.txt";

        chartWidth = winSize.width *3 / 5;
        auto sprite = Sprite::create("orange_edit.png");
        sprite->setScaleX(winSize.width / sprite->getContentSize().width);
        sprite->setScaleY(winSize.height / sprite->getContentSize().height);
        sprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        sprite->setPosition(Vec2(winSize.width/2,winSize.height/2));
        addChild(sprite);

        auto layerColor = LayerColor::create(Color4B(43, 177, 233, 255));
        layerColor->setContentSize(Size(winSize.width,100));
        layerColor->ignoreAnchorPointForPosition(false);
        layerColor->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
        layerColor->setPosition(Vec2(winSize.width/2,winSize.height));
        addChild(layerColor,4);

        //底部输入栏
        auto layout = Layout::create();
        layout->setSize(Size(winSize.width,100));
        layout->setAnchorPoint(Vec2::ZERO);
        layout->setPosition(Vec2::ZERO);
        layout->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
        layout->setBackGroundColor(Color3B(185,185,185));
        //消息显示栏
        _listView = ListView::create();
        _listView->setDirection(ui::ScrollView::Direction::VERTICAL);
        _listView->setTouchEnabled(true);
        _listView->setBounceEnabled(true);
        _listView->setGravity(cocos2d::ui::ListView::Gravity::BOTTOM);
//        _listView->setItemsMargin(2);
        _listView->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
        _listView->setBackGroundColor(Color3B::WHITE);
        _listView->setSize(Size(winSize.width,winSize.height-layout->getSize().height-25-100));
        _listView->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
        _listView->setPosition(Vec2(winSize.width/2,layout->getSize().height+25));
        addChild(_listView);

        //底部输入框背景
        auto bg = Sprite::create("whitebg.png");
        bg->setScaleX(winSize.width*3/5 / bg->getContentSize().width);
        bg->setScaleY(80 / bg->getContentSize().height);
        bg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
        bg->setPosition(Vec2(winSize.width/5,layout->getSize().height/2));
        layout->addChild(bg);

        //
        editText = TextField::create("","",30);
        editText->ignoreContentAdaptWithSize(false);
        editText->setSize(Size(winSize.width*3/5,80));
        editText->setTextHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
        editText->setTextVerticalAlignment(cocos2d::TextVAlignment::BOTTOM);
        editText->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
        editText->setPosition(Vec2(winSize.width/5,layout->getSize().height/2));
        editText->setColor(Color3B::BLACK);
        editText->addEventListener(CC_CALLBACK_2(ChartDemoScene::textFieldEvent, this));
        layout->addChild(editText);
        addChild(layout);

        //发送按钮
        sendBtn = ui::Button::create("orange_edit.png");
        sendBtn->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
        sendBtn->setPosition(Vec2(winSize.width - 20,layout->getSize().height/2));
        addChild(sendBtn);

        sendBtn->addTouchEventListener(CC_CALLBACK_2(ChartDemoScene::touchEvent,this));

        auto alert_Text = ui::Text::create();
        alert_Text->setSize(Size(50, 200));
        alert_Text->setTag(Alert_Tag);
        alert_Text->setOpacity(0);
        alert_Text->setFontSize(35);
        char* userdata = "11";
        alert_Text->setUserData(userdata);
        alert_Text->setColor(Color3B::BLACK);
        alert_Text->setPosition(Vec2(winSize.width/2,winSize.height/8));
        addChild(alert_Text);

        bRet = true;
    }while(0);
    return bRet;
}

Size ChartDemoScene::calculateFontSize(const char *str )
{
	std::string tempString = str;
	log("tempString = %s",tempString.c_str());
	size_t computeCount = tempString.size();       //如果字符串很长每次抽取100个字符来计算size;
	Size size = Size(0,0);
	for (int i = 0; i<computeCount ;)
	{
        std::string substring =  tempString.substr(i,1);
		if ((substring.c_str()[0] & 0x80 )!=0) //是汉字
		{
			substring = tempString.substr(i , 3);
			i += 3;
		}
		else
		{
			i++;
		}
		//CCLog("subString  = %s ",substring.c_str());
        auto tempLabel = LabelTTF::create(substring.c_str(),"",25);
        tempLabel->setHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
		Size tmpLsize = tempLabel->getContentSize();
		size.width+=tmpLsize.width;
	}

	float fHeight= 0;
	if( size.width > chartWidth)
	{
        fHeight = (size.width / 200 );
	}
	int nHeight =  ceil(fHeight);

	if (nHeight == 0)
	{
		nHeight = 1;
	}

	Size labelSize ;
	if (size.width < chartWidth)
	{
		labelSize = Size(size.width,nHeight*32);
	}
	else
	{
        labelSize = Size(chartWidth,nHeight*28);
	}

	//CCLog("labelSize = (%f, %f)",labelSize.width ,labelSize.height);
	//CCLog("fHeight = %f  nHeight = %d " ,fHeight ,nHeight);
	return labelSize;
}

void ChartDemoScene::touchEvent(cocos2d::Ref *pSender, Widget::TouchEventType type)
{
    switch (type) {
        case ui::Widget::TouchEventType::ENDED:
        {
            if(str.empty())
            {
                showAlertShow("输入内容不能为空");
                break;
            }

            log("str = %s",str.c_str());

            auto time = getcurrMonthTime().c_str();
            auto text = Text::create(time, "", 30);
            text->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
            text->setColor(Color3B::BLACK);

            auto _timeLayout = Layout::create();
            _timeLayout->setSize(Size(_listView->getSize().width,60));
            _timeLayout->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);

            auto rp_time = RelativeLayoutParameter::create();
            rp_time->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
            text->setLayoutParameter(rp_time);
            _timeLayout->addChild(text);
            _listView->pushBackCustomItem(_timeLayout);

            if(count % 2)
           {
                count++;
                auto _lLayout = Layout::create();
                _lLayout->setSize(Size(_listView->getSize().width,80));
                _lLayout->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);

               auto _lHeader = ui::ImageView::create("logo_douban.png");
               _lHeader->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
               _lHeader->setScale(0.5);

               auto rp_l = RelativeLayoutParameter::create();
               rp_l->setMargin(Margin(10,0,0,0));
               rp_l->setRelativeName("head");
               rp_l->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL);
               _lHeader->setLayoutParameter(rp_l);
               _lLayout->addChild(_lHeader);

                auto _lTextContent = TextField::create("","",30);
                _lTextContent->ignoreContentAdaptWithSize(false);
               auto size = calculateFontSize(str.c_str());
               _lTextContent->setSize(size);
               _lTextContent->setTextHorizontalAlignment(cocos2d::TextHAlignment::LEFT);

               if(size.width < chartWidth)
               {
                   _lTextContent->setTextHorizontalAlignment(cocos2d::TextHAlignment::RIGHT);
               }
                _lTextContent->setTextVerticalAlignment(cocos2d::TextVAlignment::CENTER);
                _lTextContent->setTouchEnabled(false);
               _lTextContent->setTag(T_Tag);
                _lTextContent->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
                _lTextContent->setColor(Color3B::BLACK);
                _lTextContent->setText(str);
               _lLayout->addChild(_lTextContent);
               str.clear();

               auto rp_t = RelativeLayoutParameter::create();
               rp_t->setMargin(Margin(10,0,0,0));
               rp_t->setRelativeToWidgetName("head");
               rp_t->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER);
               _lTextContent->setLayoutParameter(rp_t);

               _listView->pushBackCustomItem(_lLayout);

           }else
           {
               count++;
                auto _rLayout = Layout::create();
                _rLayout->setSize(Size(_listView->getSize().width,80));
                _rLayout->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);

               auto _rHeader = ui::ImageView::create("logo_mingdao.png");
               _rHeader->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
               _rHeader->setScale(0.5);

               auto rp_r = RelativeLayoutParameter::create();
               rp_r->setMargin(Margin(0,0,10,0));
               rp_r->setRelativeName("head");
               rp_r->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL);
               _rHeader->setLayoutParameter(rp_r);
               _rLayout->addChild(_rHeader);

                auto _rTextContent = TextField::create("","",30);
               _rTextContent->setTag(T_Tag);
                _rTextContent->ignoreContentAdaptWithSize(false);
               auto size = calculateFontSize(str.c_str());
               _rTextContent->setSize(size);
               _rTextContent->setTextHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
                _rTextContent->setTextVerticalAlignment(cocos2d::TextVAlignment::CENTER);
                _rTextContent->setTouchEnabled(false);
                _rTextContent->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
                _rTextContent->setColor(Color3B::BLACK);
                _rTextContent->setText(str);
               str.clear();
               auto rp_l = RelativeLayoutParameter::create();
               rp_l->setMargin(Margin(0,0,10,0));
               rp_l->setRelativeToWidgetName("head");
               rp_l->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER);
               _rTextContent->setLayoutParameter(rp_l);
               _rLayout->addChild(_rTextContent);

               _listView->pushBackCustomItem(_rLayout);
           }

//            if(_listView->getContentSize().height > _listView->getSize().height)
//            {
                _listView->scrollToBottom(0.5, true);
//            }
            editText->setText("");

            if(count > 3 )
            {
                _listView->removeItem(0);//删除时间标签,此时Item(0)就是内容标签
                auto temp = static_cast<Layout*>(_listView->getItem(0));//获取当前内容标签
                if(_text)
                {
                    removeChild(_text);
                }
                _text = static_cast<TextField*>(temp->getChildByTag(T_Tag));//获取内容标签
                log("string = %s", _text->getStringValue().c_str());//获取内容
                log("file_path = %s",file_path.c_str());
                FILE* fp = std::fopen(file_path.c_str(),"at+");
                CCASSERT(fp != NULL, "file open error");
                length = _text->getStringValue().length();
                log("length = %lu",length);
                fwrite(_text->getStringValue().c_str(), length, 1, fp);
                fclose(fp);

                _listView->removeItem(0);
            }

            break;
        }
        default:
            break;
    }
}

void ChartDemoScene::textFieldEvent(cocos2d::Ref *pSender, TextField::EventType type)
{
    switch (type)
    {
        case TextField::EventType::ATTACH_WITH_IME:
        {
            TextField* textField = dynamic_cast<TextField*>(pSender);
            Size widgetSize = winSize;
            runAction(CCMoveTo::create(0.225f,Vec2(0, widgetSize.height / 2.0f)));
            textField->setTextHorizontalAlignment(TextHAlignment::LEFT);
            textField->setTextVerticalAlignment(TextVAlignment::TOP);
        }
            break;

        case TextField::EventType::DETACH_WITH_IME:
        {
            TextField* textField = dynamic_cast<TextField*>(pSender);
            Size widgetSize = winSize;
            runAction(CCMoveTo::create(0.175f, Vec2(0, 0)));
            textField->setTextHorizontalAlignment(TextHAlignment::LEFT);
            textField->setTextVerticalAlignment(TextVAlignment::CENTER);

            str = editText->getStringValue().c_str();
            str += "\n";
        }
            break;

        case TextField::EventType::INSERT_TEXT:
            break;

        case TextField::EventType::DELETE_BACKWARD:
            break;

        default:
            break;
    }

}

void ChartDemoScene::showAlertShow(std::string contentString)
{
    auto alert = dynamic_cast<ui::Text*>(getChildByTag(Alert_Tag));
    if (alert->getUserData() == "11") {
        alert->setOpacity(255);
        char* userdata = "110";
        alert->setUserData(userdata);

        alert->setString(contentString);
        auto call_func = CallFunc::create(CC_CALLBACK_0(ChartDemoScene::alertCallback, this,alert));
        auto seq = Sequence::create(FadeOut::create(1.0f),call_func, NULL);
        alert->runAction(seq);
    }

}

void ChartDemoScene::alertCallback(ui::Text* alert)
{

    alert->setString("");
    char* userdata = "11";
    alert->setUserData(userdata);

}

std::string ChartDemoScene::getcurrTime()//获取当前年月日
{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    struct timeval now;
    struct tm* time;

    gettimeofday(&now, NULL);

    time = localtime(&now.tv_sec);
    int year = time->tm_year + 1900;

    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    return StringUtils::format("%s",date);

#endif

#if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

    struct tm* tm;
    time_t timep;
    time(timep);

    tm = localtime(&timep);
    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    return StringUtils::format("%s",date);

#endif

}

std::string ChartDemoScene::getcurrMonthTime()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    struct timeval now;
    struct tm* time;

    gettimeofday(&now, NULL);

    time = localtime(&now.tv_sec);
    int year = time->tm_year + 1900;
    log("year = %d",year);

    char date1[24] = {0};
    char date[8] = {0};
    sprintf(date1, "%d:%02d:%02d:%02d:%02d:%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday,time->tm_hour,time->tm_min,time->tm_sec);
    sprintf(date, "%02d:%02d",time->tm_hour,time->tm_min);
    return StringUtils::format("%s",date);

#endif

#if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

    struct tm* tm;
    time_t timep;
    time(timep);

    tm = localtime(&timep);
    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    return StringUtils::format("%s",date);

#endif

}

void ChartDemoScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
时间: 2024-10-13 14:58:38

Cocos2d-x计算字符串Size方法的相关文章

shell计算字符串长度方法及速度比较

chars=`seq -s " " 100`; echo $chars  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 7

计算文字size的方法

在我们开发过程中,不免要遇到计算文字的frame的时候,那文字的frame到底是如何来计算呢?今天我给大字介绍一个方法,用来计算文字的size希望对你的开发有帮助: 注:我这段代码大家可以直接拿过来用,只需要调用这个方法即可,真正的干货啊! //计算文字size的方法 /** * 计算文字的size * * @param text 要计算的文字 * @param maxtextSize 设置文字的最大范围 * @param font 设置文字的字号 * * @return 文字的size */

黑马程序员-OC特有语法:分类category,给NSString增加方法计算字符串中数字的个数

1:分类的使用场景:想对一个类,扩充一些功能,而又不改变原来类的模型,也不用继承,这时OC中的特有语法:分类可以做到: 当然分类也是一个类,也需要声明和实现,声明在.h文件中,实现在.m文件中,格式如下 // 声明 @interface  类名  (分类名称) @end // 实现 @implementation 类名 (分类名称) @end 2:分类的好处,当一个类比较庞大时,不同的部分可以放到不同的分类中,也方便团队中类的开发: 3:分类使用注意: a:分类不能增加成员变量,只能对原类增加方

c# 计算字符串和文件的MD5值的方法

快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中. <HTML开发Mac OS App 视频教程> 土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/ 百度网盘同步:http://pan.baidu.com/s/1jG1Q58M 分享  [中文纪录片]互联网时代   http://pan.baidu.com/s/1qWkJfcS 官方QQ群:(申请加入,说是我推荐的) App实践出真知 4

关于UIFont和计算字符串的高度和宽度

转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ systemFontOfSize:+ boldSystemFontOfSize:+ italicSystemFontOfSize:3.获得可用的Font Names:+ familyNames+ fontNamesForFamilyName:4.获得Font Name属性:familyName 属性 和

简单计算字符串的高度

计算字符串的高度有很多种,这里写下最常用的简单计算字符串的高度 // // NSString+NSStringExt.h // UIFontSize // // Created by mac on 15/11/14. // Copyright (c) 2015年 叶炯. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface NSString (NSSt

iOS 根据字符串样式、字符串长度计算字符串所占大小

通过Category为NSString添加方法: 1. 根据字符串样式.字符串长度计算字符串所占大小 /** *  @param font     字符串样式 *  @param maxWidth 指定字符串长度 */- (CGSize)sizeWithFont:(UIFont *)font maxWidth:(CGFloat)maxWidth { // 获取文字样式 NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; a

Cocos2d-x串算出Size方法

项目需要,根据所输入的字符串,的需要计算串帐户Size. 包代码如下面.只需要传递一个字符串,您可以返回Size: Size ChartDemoScene::calculateFontSize(const char *str ) { std::string tempString = str; log("tempString = %s",tempString.c_str()); size_t computeCount = tempString.size(); //假设字符串非常长每次抽取

【java算法】---余弦相似度计算字符串相似率

余弦相似度计算字符串相似率 功能需求:最近在做通过爬虫技术去爬取各大相关网站的新闻,储存到公司数据中.这里面就有一个技术点,就是如何保证你已爬取的新闻,再有相似的新闻 或者一样的新闻,那就不存储到数据库中.(因为有网站会去引用其它网站新闻,或者把其它网站新闻拿过来稍微改下内容就发布到自己网站中). 解析方案:最终就是采用余弦相似度算法,来计算两个新闻正文的相似度.现在自己写一篇博客总结下. 一.理论知识 先推荐一篇博客,对于余弦相似度算法的理论讲的比较清晰,我们也是按照这个方式来计算相似度的.网