首页 > 解决方案 > React Native - 我如何以粗体字重分享消息?

问题描述

我正在使用 react native Share API,我想以粗体字重分享一条消息我似乎找不到办法做到这一点有谁知道如何做到这一点?

标签: javascriptreactjsreact-native

解决方案


我还没有找到任何关于共享粗体文本的信息,但您可以在此处使用 Unicode 字符中已分配的粗体字符:

https://unicode.org/charts/nameslist/n_1D400.html

我使用此方法制作了将字符转换为粗体的函数:

const toBold = text =>{
  const charSet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '?', '.', ',', '"', "'"];
  const targetCharSet = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '❗', '❓', '.', ',', '"', "'"];
  const textArray = text.split('');
  let boldText = '';
  textArray.forEach((letter) => {
    const index = charSet.findIndex((_letter) => _letter === letter);
    if (index !== -1) {
      boldText = boldText + targetCharSet[index];
    } else {
      boldText = boldText + letter;
    }
  });
  return boldText;
}

用法:

toBold('Hi Mom');

输出:

 

注意: 文本可能不会显示在您的控制台中。


推荐阅读