样式上RN 最弱,所以要以 RN 的约束来管理样式,同时兼顾小程序的限制

0 动态修改样式

统一使用classnames库,进行样式动态的添加和删除,可以兼容各端

  1. <Text
  2. className={classNames('list__item-btm-usrInfo-flow',
  3. item.followed && 'list__item-btm-usrInfo-flow--active')}
  4. onClick={this.onFollowClick.bind(this, index)}
  5. >{item.followed ? '已关注' : '关注'}</Text>

1 RN端只支持 Flex 布局

暂时无解,因此工程布局必须全部使用flex布局

2 RN端只支持类选择器

仅支持css的类选择器,其他所有css选择器一律不支持
不过可以充分利用scss的’&’父选择器标识符来大幅简化css命名的长度

  1. .searchBar{
  2. &__rightWrap{
  3. display: flex;
  4. height: 70px;
  5. justify-content: space-between;
  6. flex-direction: column;
  7. align-items: center;
  8. &-btn{
  9. font-size: 20px;
  10. line-height: 20px;
  11. color: #333333;
  12. }
  13. }
  14. }

以上可直接在jsx标签中使用这三个样式:’searchBar’,’searchBarrightWrap’,’searchBarrightWrap-btn’

3 统一类名的书写规范

由于只能使用类选择器,如何管理大量的类名就很重要
工程样式类名统一遵循BEM规范
block__element-childElement—modifier

4 指定平台样式的条件编译

4.1 样式文件条件编译

  • index.scss
    - index.rn.scss
    平台会自动找到并引用对应后缀的样式文件,适用于页面有大面积样式不兼容的情况

    4.2 样式代码条件编译

    1. .read{
    2. font-size: 18px;
    3. /* #ifdef rn */ RN端保留的样式
    4. font-size: 22px;
    5. /* #endif */
    6. /* #ifndef rn */ RN端剔除的样式
    7. white-space:nowrap;
    8. text-overflow:ellipsis;
    9. /* #endif */
    10. }

    5 RN端不支持background-image

    使用position:realtive/absolute 配合兼容多端的样式解决方案 - 图1标签

    6 RN端不支持border-xxx的简写

    不支持简写如border-top等,但是原始拆开的写法是支持的,因此使用scss定义一个mixin统一处理:

    1. @mixin border($dir, $width, $style, $color) {
    2. border: 0 $style $color;
    3. @each $d in $dir {
    4. #{border-#{$d}-width}: $width;
    5. }
    6. }

    使用时:

    1. .item-footer {
    2. display: flex;
    3. flex-direction: row;
    4. height: 100%;
    5. background: #faf9fa;
    6. @include border(top, 1px, solid, $border-color);
    7. }

    7 h5端的1px的圆角边框不显示

    h5中的1px转成rem后小于0.5px,如果此时同时存在border-radius样式,则导致边框不显示,解决方案:
    定义mixin,把h5的1px全部写成1PX,此时px就不会被转成rem,而是继续保持px单位,就可以显示了 ```css @mixin hairline($attr) {

    {$attr}: 1px;

    @include eject($attr, 1PX);//RN端不支持PX,因此要剔除 }

@mixin eject($attr, $value) { /postcss-pxtransform rn eject enable/

{$attr}: $value;

/postcss-pxtransform rn eject disable/ }

  1. <a name="gUEW5"></a>
  2. ## 8 文本超出限制显示省略号
  3. RN端:<Text numberOfLines={1} style={styles.previewText}>{preview}</Text><br />H5/小程序端: -webkit-line-clamp
  4. ```css
  5. @mixin lamp-clamp($line) {
  6. /*postcss-pxtransform rn eject enable*/
  7. overflow: hidden;
  8. text-overflow: ellipsis;
  9. display: -webkit-box;
  10. -webkit-line-clamp: $line;
  11. /* autoprefixer: ignore next */
  12. -webkit-box-orient: vertical;
  13. /*postcss-pxtransform rn eject disable*/
  14. }

9 RN端不支持display:none

设置height为0迂回实现

10 RN端不支持position:fixed

父容器相对布局,需要固定的组件绝对布局,固定其位置,需要滑动的组件使用组件,并提前计算好高度

11 获取屏幕剩余可用高度

抽取工具方法

  1. /**
  2. showTabBar 是否使用了默认标题栏
  3. fixPx 固定部分已占用的高度
  4. /*
  5. export function getWindowHeight(showTabBar = true,fixPx = 0) {
  6. const info = Taro.getSystemInfoSync()
  7. const { windowHeight, statusBarHeight, titleBarHeight } = info
  8. const tabBarHeight = showTabBar ? TAB_BAR_HEIGHT : 0
  9. if (process.env.TARO_ENV === 'rn') {
  10. return windowHeight - statusBarHeight - NAVIGATOR_HEIGHT - tabBarHeight - fixPx/2
  11. }
  12. if (process.env.TARO_ENV === 'h5') {
  13. return `${windowHeight - tabBarHeight - fixPx/2}px`
  14. }
  15. if (process.env.TARO_ENV === 'alipay') {
  16. // NOTE 支付宝比较迷,windowHeight 似乎是去掉了 tabBar 高度,但无 tab 页跟 tab 页返回高度是一样的
  17. return `${windowHeight - statusBarHeight - titleBarHeight + (showTabBar ? 0 : TAB_BAR_HEIGHT) - topPx/2}px`
  18. }
  19. return `${windowHeight - fixPx/2}px`
  20. }

12 RN不支持标签样式


但是支持样式变量的写法:

13 不支持float浮动

使用flex布局迂回实现

14 隐藏各端的默认标题栏

入口文件修改窗口配置,样式改为costom,即可自定义标题栏

  1. config = {
  2. window: {
  3. navigationStyle:'custom'
  4. }
  5. }

15 RN不支持渐变色

可以针对RN端引入额外的渐变色支持库,自己没有尝试,目前最简单的解决方案是美工切图

16 RN不支持通过Image的border-radius来实现圆形

给图片加一个View标签容器包裹,RN支持设置View的border-radius来实现圆形,保证内部图片尺寸和外部蒙层一致,即可实现圆形图片效果

其他RN不支持的样式

*{}通配符,厂商前缀:-webkit-,text-overflow,normal 或 bold 之外的 font-weight,filter,white-space,text-overflow,box-shadow……太多了
凡是RN的样式规范里没有的,都不支持
需要见招拆招