flutter 文字大小测量

测试flutter项目下txt库是否能正常工作,本demo测试文本测量功能。

#include<stdio.h>
#include<iostream>
#include <sstream>
#include <string>
#include "txt/font_style.h"
#include "txt/font_weight.h"
#include "txt/paragraph_builder_txt.h"
#include "txt/paragraph_txt.h"
#include "txt/placeholder_run.h"
#include "txt/typeface_font_asset_provider.h"
#include "flutter/fml/icu_util.h"

std::shared_ptr<txt::FontCollection> GetTestFontCollection() {
	std::unique_ptr<txt::TypefaceFontAssetProvider> font_provider = std::make_unique<txt::TypefaceFontAssetProvider>();
  	font_provider->RegisterTypeface(SkTypeface::MakeFromFile("/home/client/flutter/engine/flutter/third_party/txt/third_party/fonts/Roboto-Black.ttf"));
  	std::shared_ptr<txt::FontCollection> collection = std::make_shared<txt::FontCollection>();
  	collection->SetAssetFontManager(sk_make_sp<txt::AssetFontManager>(std::move(font_provider)));
  	return collection;
}

void testText(){
    const char* text = "Hello World Text Dialog";
    auto icu_text = icu::UnicodeString::fromUTF8(text);
    std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length());

     txt::ParagraphStyle paragraph_style;
    txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());

    txt::TextStyle text_style;
    // We must supply a font here, as the default is Arial, and we do not
    // include Arial in our test fonts as it is proprietary. We want it to
    // be Arial default though as it is one of the most common fonts on host
    // platforms. On real devices/apps, Arial should be able to be resolved.
    text_style.font_families = std::vector<std::string>(1, "Roboto");
    text_style.color = SK_ColorBLACK;
    builder.PushStyle(text_style);
    builder.AddText(u16_text);

    builder.Pop();
    auto paragraph = std::unique_ptr<txt::ParagraphTxt>(
      static_cast<txt::ParagraphTxt*>(builder.Build().release()));
    paragraph->Layout(500);

    printf("text line:%d\n",paragraph->GetLineCount());
    printf("text width:%f\n",paragraph->GetLongestLine(),paragraph->GetMaxIntrinsicWidth());

}

int main(){
	fml::icu::InitializeICU("icudtl.dat");
	testText();
    return 0;
}

输出内容:

text line:1
text width:153.554688
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter中,可以使用Text部件来设置文本的字体大小。通过在TextStyle中设置fontSize属性,可以指定文本的字体大小。例如,TextStyle(fontSize: 20)表示文本的字体大小为20。 另外,Flutter还提供了AutoSizeText部件,它与Text部件非常相似,唯一的区别是它可以自动调整文本大小以适应其边界。您可以通过设置minFontSize和maxFontSize参数来限制字体大小的范围。例如,AutoSizeText('A really long String', style: TextStyle(fontSize: 30), minFontSize: 18, maxLines: 4, overflow: TextOverflow.ellipsis)表示文本的字体大小范围为18到30之间,同时最多显示4行文本,并在超出边界时使用省略号进行截断。 另外,如果您在使用Text部件对中文设置字体大小后,在一个Container容器中字体不居中的问题,您可以使用strutStyle的forceStrutHeight参数来强制lineHeight与字体的高度一致,从而解决字体在容器中的居中问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【FlutterFlutter auto_size_text 文本自适应大小](https://blog.csdn.net/diandianxiyu/article/details/132158756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [flutter_manhuatai:使用 Flutter 开发漫花台漫画 app](https://download.csdn.net/download/weixin_42128558/19254872)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值