在React Native中更改按钮字体大小

21

我正在尝试在我的React Native应用中更改按钮字体大小,但是出现了错误。有人知道如何正确地做吗? 谢谢。


2
你好!你遇到了什么错误? - Michael Hancock
4
请发布你的代码。 - Colin Ricardo
我想为导航按钮设置样式。我在 Snack 上有代码,这是链接:https://snack.expo.io/@indriruth/navigation - webprogramming13
我尝试在./components/Home.js中更改按钮的fontSize和fontWeight。谢谢你们的回应。 - webprogramming13
6个回答

18

您可以使用带有titleStyle属性的react-native-elements组件。

import {Input, Button} from 'react-native-elements';

<Button
   onPress={this.addPicture}
   titleStyle={{
       color: "white",
       fontSize: 16,
   }}
   buttonStyle={{
       backgroundColor: "white",
       borderRadius: 60,
       flex: 1,
       height: 30,
       width: 30,  
   }}

   title="+"
/>

哈哈哈,我终于在不知道搜索了多久之后让 buttonStyle 正常工作了,然后我还得使用一个单独的属性来设置标题...谢谢。 - velkoon

9

很遗憾,根据文档(https://reactnative.dev/docs/button),您不能改变按钮的字体大小。您可以更改的唯一样式属性是color

<Button
  onPress={onPressLearnMore}
  title="Learn More"
  color="#841584"
  accessibilityLabel="Learn more about this purple button"
/>

7
我有一种感觉,你在Touchable中没有使用Text元素:
import React from 'react';
import { TouchableWithoutFeedback, Text } from 'react-native';

export default function ComponentName() {
  return (
    <TouchableWithoutFeedback>
      <Text style={{ fontSize: 24 }}>Button Text</Text>
    </TouchableWithoutFeedback>
  );
}

我认为我不能使用Touchable来导航到下一页。我在上面发布了链接和更多描述我的问题。请检查一下。 - webprogramming13

4

以下是我对使用 TouchableOpacityText 轻松样式化按钮的解决方案:

import React, { Component } from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from "react-native";

export default class CustomButton extends Component {
  render(){
    return (
      <View style={styles.container}>

        /* Custom Button */
        <TouchableOpacity
          style={styles.customBtnBG}
          onPress={() => {}} 
        >
          <Text style={styles.customBtnText}>Button</Text>
        </TouchableOpacity>

      </View>
    )
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
        justifyContent: "center",
    alignItems: "center"
  },

  /* Here style the text of your button */
    customBtnText: {
        fontSize: 40,
        fontWeight: '400',
        color: "#fff",
    },

  /* Here style the background of your button */
    customBtnBG: {
    backgroundColor: "#007aff",
    paddingHorizontal: 30,
    paddingVertical: 5,
    borderRadius: 30
    }
});

1
使用这个库https://github.com/APSL/react-native-button代替React Native中的Button组件。
<View>
  <Button
    style={{
      backgroundColor: "#FE434C",
      borderColor: "transparent",
      borderRadius: 20,
      width: 250
    }}
    textStyle={{ color: "#FFFFFF", fontSize: 20 }}
  >
    Hello
  </Button>
</View>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接