LoginSignup
0
0

More than 3 years have passed since last update.

ReactNativeでテキストボタンまたはテキストリンクのタップ領域を拡大する

Posted at

一部リンクがあるようなテキストの場合タップ領域がかなりシビアで正確にタップしないと押すことが出来ない。

const App = () => {
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.row}>
        <Text>パスワードを忘れた方は</Text>
        <TouchableOpacity
          onPress={() => {
            console.log('tap');
          }}>
          <Text style={styles.link}>こちら</Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    margin: 8,
  },
  row: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
  },
  link: {
    textDecorationLine: 'underline',
    color: 'blue',
  },
});

<TouchableOpacity>のstyleにpaddingを設定してタップ領域を広げることができるが、paddingで広がった分、他の領域に影響を与えてしまいデザインが崩れてしまったりしてしまう。

  <TouchableOpacity
    style={{borderColor: 'gray', borderWidth: 1, padding: 8}}
    onPress={() => {
      console.log('tap');
    }}>   

こういうときはpaddingで設定した値を負のmarginとして付与してあげればよい。

  <TouchableOpacity
    style={{borderColor: 'gray', borderWidth: 1, padding: 8, margin: -8}}
    onPress={() => {
      console.log('tap');
    }}>        

こうすることで他の領域に影響を与えずにテキストリンクのタップ領域を拡大することができる。

参考:
ボタンやアイコンやナビゲーションなど、クリック・タップ可能な領域のサイズを広くする実装方法のまとめ
テキストだけのボタンは、なぜスマホでのユーザビリティを損なうのか
Text margin, padding not working if in another Text component #6728

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0