{"id":485,"date":"2023-02-25T14:04:53","date_gmt":"2023-02-25T06:04:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=485"},"modified":"2023-04-29T20:51:59","modified_gmt":"2023-04-29T12:51:59","slug":"react-native-learning-text-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-text-component\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bText\u7ec4\u4ef6"},"content":{"rendered":"<p>Text\u7ec4\u4ef6\u4e3b\u8981\u7528\u4e8e\u663e\u793a\u6587\u672c\uff1a<\/p>\n<ul>\n<li>\u5177\u6709\u54cd\u5e94\u6027\uff1a\u53ef\u4ee5\u76f8\u5e94\u70b9\u51fb\u7b49\u4e8b\u4ef6<\/li>\n<li>\u53ef\u4ee5\u5d4c\u5957\uff1aText\u6807\u7b7e\u4e2d\u53ef\u4ee5\u5d4c\u5957Text\u6807\u7b7e<\/li>\n<li>\u53ef\u4ee5\u7ee7\u627f\u6837\u5f0f\uff1a\u5185\u90e8Text\u7ec4\u4ef6\u53ef\u4ee5\u7ee7\u627f\u5916\u90e8Text\u7ec4\u4ef6\u7684\u6837\u5f0f<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>Text\u7ec4\u4ef6\u7684\u7279\u6027\uff1a<\/p>\n<ul>\n<li><code>onPress<\/code><\/li>\n<li><code>numberOfLines<\/code>: \u6700\u591a\u663e\u793a\u591a\u5c11\u884c<\/li>\n<li><code>onLayout<\/code><\/li>\n<\/ul>\n<p>\u6848\u4f8b\uff1a\u7f51\u6613\u65b0\u95fb\u5ba2\u6237\u7aef Text\u7ec4\u4ef6\u5b9e\u73b0<\/p>\n<p>\u7ec4\u4ef6\u7684\u9897\u7c92\u5ea6\u8bbe\u8ba1\u4e3b\u8981\u53d6\u51b3\u4e8e\u5e94\u7528\u7684\u7ed3\u6784\u8bbe\u8ba1<\/p>\n<h3>\u5934\u90e8\u7ec4\u4ef6<\/h3>\n<p>\u5355\u72ec\u5c01\u88c5 \u72ec\u7acb\u6210\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>ES5\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/\u5bfc\u51fa\nmodule.exports=Header;`\n\n\/\/\u5f15\u7528\nconst Header=require(&#039;.\/header&#039;);<\/code><\/pre>\n<p>\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\nimport React, {\n    AppRegistry,\n    Component,\n    StyleSheet,\n    PixelRatio,\n    Text,\n    View\n    } from &#039;react-native&#039;;\n\nclass Header extends Component{\n    render(){\n        return (\n            &lt;View style={styles.flex}&gt;\n                &lt;Text style={styles.font}&gt;\n                    &lt;Text style={styles.font_1}&gt;\u7f51\u6613&lt;\/Text&gt;\n                    &lt;Text style={styles.font_2}&gt;\u65b0\u95fb&lt;\/Text&gt;\n                    &lt;Text&gt;\u6709\u6001\u5ea6&quot;&lt;\/Text&gt;\n                &lt;\/Text&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    flex:{\n        marginTop:25,\n        height:50,\n        borderBottomWidth:3\/PixelRatio.get(),\n        borderBottomColor:&#039;#EF2D36&#039;,\n        alignItems:&#039;center&#039;,\n    },\n    font:{\n        fontSize:25,\n        fontWeight:&#039;bold&#039;,\n        textAlign:&#039;center&#039;,\n    },\n    font_1:{\n        color:&#039;#CD1D1C&#039;,\n    },\n    font_2:{\n        color:&#039;#FFF&#039;,\n        backgroundColor:&#039;#CD1D1C&#039;,\n    },\n});\n\nmodule.exports=Header;<\/code><\/pre>\n<h3>\u5217\u8868\u7ec4\u4ef6<\/h3>\n<pre><code class=\"language-javascript\">\/\/\u7ec4\u4ef6\u8c03\u7528\n&lt;List title=&#039;\u4e00\u7ebf\u57ce\u5e02\u697c\u5e02\u9000\u70e7 \u6709\u623f\u6e90\u4e00\u591c\u8dcc\u4ef7160\u4e07&#039;&gt;&lt;\/List&gt;\n\n\/\/\u7ec4\u4ef6\u5b9a\u4e49\nclass List extends Component{\n    render(){\n        return (\n            &lt;View style={styles.list_item}&gt;\n               &lt;Text style={styles.list_item_font}&gt;{this.props.title}&lt;\/Text&gt;\n            &lt;\/View&gt;\n        );\n    }\n}<\/code><\/pre>\n<h3>\u91cd\u8981\u65b0\u95fb\u7ec4\u4ef6<\/h3>\n<pre><code class=\"language-javascript\">\/\/\u7ec4\u4ef6\u8c03\u7528\n&lt;ImportantNews\n    news={[\n        &#039;\u89e3\u653e\u519b\u62a5\u62a5\u793e\u5927\u697c\u6b63\u5728\u62c6\u9664 \u6807\u8bc6\u5df2\u88ab\u5378\u4e0b(\u56fe)&#039;,\n        &#039;\u97e9\u56fd\u505c\u7b7e\u4e1c\u4e09\u770152\u5bb6\u65c5\u884c\u793e \u6216\u4e3a\u963b\u6b62\u671d\u65c5\u6e38\u521b\u6c47&#039;,\n        &#039;\u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8\uff0c\u6709\u5973\u751f\u732e\u521d\u543b\n        \u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8\uff0c\u6709\u5973\u751f\u732e\u521d\u543b\n        \u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8\uff0c\u6709\u5973\u751f\u732e\u521d\u543b&#039;,\n        &#039;\u9632\u603b\u90e8\u7f72\u957f\u6c5f\u9632\u6c5b:\u4ee5\u9632\u5fa198\u5e74\u91cf\u7ea7\u5927\u6d2a\u6c34\u4e3a\u76ee\u6807&#039;\n    ]}&gt;\n&lt;\/ImportantNews&gt;\n\n\/\/\u7ec4\u4ef6\u5b9a\u4e49\nclass ImportantNews extends Component{\n    show(title){\n        alert(title);\n    }\n\n    render(){\n        var news=[];\n        for(var i in this.props.news){\n            var text=(\n                &lt;Text\n                    onPress={this.show.bind(this, this.props.news[i])}\n                    numberOfLines={2}\n                    style={styles.news_item}\n                    key={i}\n                    &gt;\n                    {this.props.news[i]}\n                &lt;\/Text&gt;\n            );\n            news.push(text);\n        }\n        return (\n            &lt;View style={styles.flex}&gt;\n                &lt;Text style={styles.news_title}&gt;\u4eca\u65e5\u8981\u95fb&lt;\/Text&gt;\n                {news}\n            &lt;\/View&gt;\n\n        );\n    }\n}<\/code><\/pre>\n<p>\u8b66\u544a\u7684\u5904\u7406\uff1a\u6570\u7ec4\u91cc\u9762\u9700\u8981key\u5c5e\u6027\uff0c\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/fb.me\/react-warning-keys\">https:\/\/fb.me\/react-warning-keys<\/a><\/p>\n<p>\u6dfb\u52a0\u8bed\u53e5 <code>key={i}<\/code> \u5373\u53ef\u89e3\u51b3<\/p>\n<h3>\u4e3b\u7a0b\u5e8f\u6e90\u7801<\/h3>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\n\nimport React, {\n    AppRegistry,\n    Component,\n    StyleSheet,\n    PixelRatio,\n    Text,\n    View\n} from &#039;react-native&#039;;\n\n\/\/const Header=require(&#039;.\/header&#039;);\nimport Header from &#039;.\/header&#039;;\n\nclass RNAPP extends Component {\n  render() {\n    return (\n        &lt;View style={styles.flex}&gt;\n            &lt;Header&gt;&lt;\/Header&gt;\n\n            &lt;List title=&#039;\u4e00\u7ebf\u57ce\u5e02\u697c\u5e02\u9000\u70e7 \u6709\u623f\u6e90\u4e00\u591c\u8dcc\u4ef7160\u4e07&#039;&gt;&lt;\/List&gt;\n            &lt;List title=&#039;\u4e0a\u6d77\u5e02\u6c11\u79f0\u5893\u5730\u592a\u8d35\u4e70\u4e0d\u8d77 \u4e70\u623f\u5b58\u9aa8\u7070&#039;&gt;&lt;\/List&gt;\n            &lt;List title=&#039;\u671d\u9c9c\u518d\u53d1\u89c6\u9891:\u6467\u6bc1\u9752\u74e6\u53f0 \u4e00\u5207\u5316\u4f5c\u7070\u70ec&#039;&gt;&lt;\/List&gt;\n            &lt;List title=&#039;\u751f\u6d3b\u5927\u7206\u70b8\u4eba\u7269\u539f\u578b\u90fd\u597d\u725b\u903c&#039;&gt;&lt;\/List&gt;\n\n            &lt;ImportantNews\n                news={[\n                &#039;\u89e3\u653e\u519b\u62a5\u62a5\u793e\u5927\u697c\u6b63\u5728\u62c6\u9664 \u6807\u8bc6\u5df2\u88ab\u5378\u4e0b(\u56fe)&#039;,\n                &#039;\u97e9\u56fd\u505c\u7b7e\u4e1c\u4e09\u770152\u5bb6\u65c5\u884c\u793e \u6216\u4e3a\u963b\u6b62\u671d\u65c5\u6e38\u521b\u6c47&#039;,\n                &#039;\u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8\uff0c\u6709\u5973\u751f\u732e\u521d\u543b\n                \u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8\uff0c\u6709\u5973\u751f\u732e\u521d\u543b\n                \u5357\u4eac\u5927\u5b66\u751f\u53d1\u8d77\u4eb2\u543b\u964c\u751f\u4eba\u6d3b\u52a8 \u6709\u5973\u751f\u732e\u521d\u543b&#039;,\n                &#039;\u9632\u603b\u90e8\u7f72\u957f\u6c5f\u9632\u6c5b:\u4ee5\u9632\u5fa198\u5e74\u91cf\u7ea7\u5927\u6d2a\u6c34\u4e3a\u76ee\u6807&#039;\n                ]}&gt;\n            &lt;\/ImportantNews&gt;\n        &lt;\/View&gt;\n    );\n  }\n}\n\nclass List extends Component{\n    render(){\n        return (\n            &lt;View style={styles.list_item}&gt;\n               &lt;Text style={styles.list_item_font}&gt;{this.props.title}&lt;\/Text&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nclass ImportantNews extends Component{\n    show(title){\n        alert(title);\n    }\n\n    render(){\n        var news=[];\n        for(var i in this.props.news){\n            var text=(\n                &lt;Text\n                    onPress={this.show.bind(this, this.props.news[i])}\n                    numberOfLines={2}\n                    style={styles.news_item}\n                    key={i}\n                    &gt;\n                    {this.props.news[i]}\n                &lt;\/Text&gt;\n            );\n            news.push(text);\n        }\n        return (\n            &lt;View style={styles.flex}&gt;\n                &lt;Text style={styles.news_title}&gt;\u4eca\u65e5\u8981\u95fb&lt;\/Text&gt;\n                {news}\n            &lt;\/View&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    flex:{\n        flex:1,\n    },\n\n    list_item:{\n        height:40,\n        marginLeft:10,\n        marginRight:10,\n        borderBottomWidth:1,\n        borderBottomColor:&#039;#ddd&#039;,\n        justifyContent:&#039;center&#039;,\n    },\n\n    list_item_font:{\n        fontSize:16,\n    },\n\n    news_item:{\n        marginLeft:10,\n        marginRight:10,\n        fontSize:15,\n        lineHeight:40,\n    },\n\n    news_title:{\n        fontSize:20,\n        fontWeight:&#039;bold&#039;,\n        color:&#039;#CD1D1C&#039;,\n        marginLeft:10,\n        marginTop:15,\n\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Text\u7ec4\u4ef6\u4e3b\u8981\u7528\u4e8e\u663e\u793a\u6587\u672c\uff1a \u5177\u6709\u54cd\u5e94\u6027\uff1a\u53ef\u4ee5\u76f8\u5e94\u70b9\u51fb\u7b49\u4e8b\u4ef6 \u53ef\u4ee5\u5d4c\u5957\uff1aText\u6807\u7b7e\u4e2d\u53ef\u4ee5\u5d4c\u5957Text\u6807\u7b7e  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-485","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=485"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}