TextView.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * 自定义Text组件
  3. * @邠心vbe on 2023/08/30
  4. */
  5. import React from 'react';
  6. import { Text, View } from 'react-native';
  7. import app from '../../app.json';
  8. const getRadius = (style, fixedAlign) => {
  9. let s = undefined;
  10. if (Array.isArray(style)) {
  11. let res = {}
  12. for (let s of style) {
  13. res = {...res, ...s};
  14. }
  15. s = res;
  16. } else {
  17. s = style
  18. }
  19. var view = {}, text = {}
  20. for (let name in s) {
  21. if (name.indexOf('margin') >= 0) {
  22. view[name] = s[name];
  23. continue;
  24. }
  25. if (name.indexOf('padding') >= 0) {
  26. view[name] = s[name];
  27. continue;
  28. }
  29. if (name.indexOf('color') == 0) {
  30. text[name] = s[name];
  31. continue;
  32. }
  33. if (name.indexOf('font') >= 0) {
  34. text[name] = s[name];
  35. continue;
  36. }
  37. if (name.indexOf('border') >= 0) {
  38. view[name] = s[name];
  39. continue;
  40. }
  41. if (name.indexOf('background') >= 0) {
  42. view[name] = s[name];
  43. continue;
  44. }
  45. if (name.indexOf('text') >= 0) {
  46. text[name] = s[name];
  47. continue;
  48. }
  49. if (name.indexOf('position') >= 0) {
  50. view[name] = s[name];
  51. continue;
  52. }
  53. if (name.indexOf('line') >= 0) {
  54. text[name] = s[name];
  55. continue;
  56. }
  57. view[name] = s[name];
  58. }
  59. if (fixedAlign) {
  60. //修复RN0.7x+渲染中文无法对齐
  61. if (view.alignItems == undefined && view.flexDirection == undefined && text.textAlign == undefined) {
  62. view.alignItems = "center";
  63. view.flexDirection = "row";
  64. }
  65. if (!text.lineHeight) {
  66. if (text.fontSize) {
  67. text.lineHeight = text.fontSize * 1.3;
  68. } else {
  69. text.fontSize = 14;
  70. text.lineHeight = 18.2;
  71. }
  72. }
  73. if (text.fontSize && app.isLumiWhitelabel) {
  74. text.fontSize -= 2;
  75. }
  76. text.includeFontPadding = false;
  77. }
  78. return {
  79. view: view,
  80. text: text
  81. }
  82. }
  83. const TextView = ({style, ellipsizeMode, numberOfLines, allowFontScaling=false, onPress, onLongPress, fixedAlign=true, selectable, children}) => {
  84. const styles = getRadius(style, fixedAlign);
  85. return (
  86. <View style={styles.view}>
  87. <Text
  88. style={styles.text}
  89. allowFontScaling={allowFontScaling}
  90. ellipsizeMode={ellipsizeMode}
  91. numberOfLines={numberOfLines}
  92. selectable={selectable}
  93. onPress={onPress}
  94. onLongPress={onLongPress}>{children}</Text>
  95. </View>
  96. );
  97. };
  98. export default TextView;