react-native 樣式總結(jié)

布局

React-Native的布局是完全是用flex來實現(xiàn)。

flex的用法就不多說了,具體可參考阮一峰老師的這篇文章《flex布局:語法篇》,里面對flex的講解非常詳細;

需要注意的是:React-Native中的flex的相關(guān)屬性不是完全依照w3c規(guī)范提供的各種值,對其中的某些屬性值進行了閹割。

React-Native 的樣式基本上是實現(xiàn)了 CSS 的一個子集,并且屬性名不完全一致。

下面就借用《React-Native樣式指南》中內(nèi)容說明一下:

目錄

Properties 屬性


Text 文本(18)

屬性名 取值 描述
color <color> 對應(yīng) CSS color 屬性
fontFamily string 對應(yīng) CSS font-family 屬性
fontSize <number> 對應(yīng) CSS font-size 屬性
fontStyle normal, italic 對應(yīng) CSS font-style 屬性,但閹割了 oblique 取值
fontWeight normal, bold 100~900 對應(yīng) CSS font-weight 屬性,但閹割了 bolder, lighter 取值
lineHeight <number> 對應(yīng) CSS line-height 屬性
textAlign auto, left, right, center, justifyiOS 對應(yīng) CSS text-align 屬性,但增加了 auto 取值。當(dāng)取值為 justify 時,在 Android 上會變?yōu)?left
textDecorationLine none, underline, line-through, underline line-through 對應(yīng) CSS text-decoration-line 屬性,但閹割了 overline, blink 取值
textShadowColor <color> 對應(yīng) CSS text-shadow 屬性中的顏色定義
textShadowOffset {
width:<number>,
height:<number>
}
對應(yīng) CSS text-shadow 屬性中的陰影偏移定義
textShadowRadius <number> CSS 中,陰影的圓角大小取決于元素的圓角定義,不需要額外定義
includeFontPadding<br />Android <bool> Android在默認情況下會為文字額外保留一些padding,以便留出空間擺放上標(biāo)或是下標(biāo)的文字。對于某些字體來說,這些額外的padding可能會導(dǎo)致文字難以垂直居中。如果你把textAlignVertical設(shè)置為center之后,文字看起來依然不在正中間,那么可以嘗試將本屬性設(shè)置為false
textAlignVertical<br />Android auto, top, bottom, center 對應(yīng) CSS vertical-align 屬性,增加了 auto 取值,center 取代了 middle,并閹割了 baseline, sub 等值
fontVariant<br />iOS small-caps, oldstyle-nums, lining-nums, tabular-nums, proportional-nums 對應(yīng) CSS font-variant 屬性,但取值更豐富
letterSpacing<br />iOS <number> 對應(yīng) CSS letter-spacing 屬性
textDecorationColor<br />iOS <color> 對應(yīng) CSS text-decoration-color 屬性
textDecorationStyle<br />iOS solid, double, dotted, dashed 對應(yīng) CSS text-decoration-style 屬性,但閹割了 wavy 取值
writingDirection<br />iOS auto, ltr, rtl 對應(yīng) CSS direction 屬性,增加了 auto 取值

<a name="dimension"></a>

Dimension 尺寸(6)

屬性名 取值 描述
width <number> 對應(yīng) CSS width 屬性
minWidth <number> 對應(yīng) CSS min-width 屬性
maxWidth <number> 對應(yīng) CSS max-width 屬性
height <number> 對應(yīng) CSS height 屬性
minHeight <number> 對應(yīng) CSS min-height 屬性
maxHeight <number> 對應(yīng) CSS max-height 屬性

<a name="positioning"></a>

Positioning 定位(6)

屬性名 取值 描述
position absolute, relative 對應(yīng) CSS position 屬性,但閹割了 static, fixed 取值
top <number> 對應(yīng) CSS top 屬性
right <number> 對應(yīng) CSS right 屬性
bottom <number> 對應(yīng) CSS bottom 屬性
left <number> 對應(yīng) CSS left 屬性
zIndex <number> 對應(yīng) CSS z-index 屬性

Margin 外部白(7)

屬性名 取值 描述
margin <number> 對應(yīng) CSS margin 屬性,不同的是,它只能定義一個參數(shù),如需分別定義上、右、下、左4個方位的外補白,可以通過下面的單向外部白屬性
marginHorizontal <number> 無對應(yīng)的 CSS 屬性。其效果相當(dāng)于同時設(shè)置 marginRightmarginLeft
marginVertical <number> 無對應(yīng)的 CSS 屬性。其效果相當(dāng)于同時設(shè)置 marginTopmarginBottom
marginTop <number> 對應(yīng) CSS margin-top 屬性
marginRight <number> 對應(yīng) CSS margin-right 屬性
marginBottom <number> 對應(yīng) CSS margin-bottom 屬性
marginLeft <number> 對應(yīng) CSS margin-left 屬性

Padding 內(nèi)部白(7)

屬性名 取值 描述
padding <number> 對應(yīng) CSS padding 屬性,不同的是,它只能定義一個參數(shù),如需分別定義上、右、下、左4個方位的內(nèi)補白,可以通過下面的單向內(nèi)部白屬性
paddingHorizontal <number> 無對應(yīng)的 CSS 屬性。其效果相當(dāng)于同時設(shè)置 paddingRightpaddingLeft
paddingVertical <number> 無對應(yīng)的 CSS 屬性。其效果相當(dāng)于同時設(shè)置 paddingToppaddingBottom
paddingTop <number> 對應(yīng) CSS padding-top 屬性
paddingRight <number> 對應(yīng) CSS padding-right 屬性
paddingBottom <number> 對應(yīng) CSS padding-bottom 屬性
paddingLeft <number> 對應(yīng) CSS padding-left 屬性

Border 邊框(20)

屬性名 取值 描述
borderStyle solid, dotted, dashed 對應(yīng) CSS border-style 屬性,但閹割了 none, hidden, double, groove, ridge, inset, outset 取值,且無方向分拆屬性
borderWidth <number> 對應(yīng) CSS border-width 屬性
borderTopWidth <number> 對應(yīng) CSS border-top-width 屬性
borderRightWidth <number> 對應(yīng) CSS border-right-width 屬性
borderBottomWidth <number> 對應(yīng) CSS border-bottom-width 屬性
borderLeftWidth <number> 對應(yīng) CSS border-left-width 屬性
borderColor <color> 對應(yīng) CSS border-color 屬性
borderTopColor <color> 對應(yīng) CSS border-top-color 屬性
borderRightColor <color> 對應(yīng) CSS border-right-color 屬性
borderBottomColor <color> 對應(yīng) CSS border-bottom-color 屬性
borderLeftColor <color> 對應(yīng) CSS border-left-color 屬性
borderRadius <number> 對應(yīng) CSS border-radius 屬性
borderTopLeftRadius <number> 對應(yīng) CSS border-top-left-radius 屬性
borderTopRightRadius <number> 對應(yīng) CSS border-top-right-radius 屬性
borderBottomLeftRadius <number> 對應(yīng) CSS border-bottom-left-radius 屬性
borderBottomRightRadius <number> 對應(yīng) CSS border-bottom-right-radius 屬性
shadowColor <color> 對應(yīng) CSS box-shadow 屬性中的顏色定義
shadowOffset {
width: <number>,
height: <number>
}
對應(yīng) CSS box-shadow 屬性中的陰影偏移定義
shadowRadius <number> CSS 中,陰影的圓角大小取決于元素的圓角定義,不需要額外定義
shadowOpacity <number> 對應(yīng) CSS box-shadow 屬性中的陰影透明度定義

<a name="background"></a>

Background 背景(1)

屬性名 取值 描述
backgroundColor <color> 對應(yīng) CSS background-color 屬性

<a name="transform"></a>

Transform 轉(zhuǎn)換(3)

屬性名 取值 描述
transform [{perspective: number}, {rotate: string}, {rotateX: string}, {rotateY: string}, {rotateZ: string}, {scale: number}, {scaleX: number}, {scaleY: number}, {translateX: number}, {translateY: number}, {skewX: string}, {skewY: string}] 對應(yīng) CSS transform 屬性
transformMatrix TransformMatrixPropType 類似于 CSStransform 屬性的 matrix()matrix3d() 函數(shù)
backfaceVisibility visible, hidden 對應(yīng) CSS backface-visibility 屬性

<a name="flexbox"></a>

Flexbox 彈性盒(9)

屬性名 取值 描述
flex <number> 對應(yīng) CSS flex 屬性,但只能為整數(shù)值
flexGrow <number> 對應(yīng) CSS flex-grow 屬性
flexShrink <number> 對應(yīng) CSS flex-shrink 屬性
flexBasis <number> 對應(yīng) CSS flex-basis 屬性
flexDirection row, row-reverse, column, column-reverse 對應(yīng) CSS flex-direction 屬性
flexWrap wrap, nowrap 對應(yīng) CSS flex-wrap 屬性,但閹割了 wrap-reverse 取值
justifyContent flex-start, flex-end, center, space-between, space-around 對應(yīng) CSS justify-content 屬性,但閹割了 stretch 取值。
alignItems flex-start, flex-end, center, stretch 對應(yīng) CSS align-items 屬性,但閹割了 baseline 取值。
alignSelf auto, flex-start, flex-end, center, stretch 對應(yīng) CSS align-self 屬性,但閹割了 baseline 取值

<a name="other"></a>

Other 其他

屬性名 取值 描述
opacity <number> 對應(yīng) CSS opacity 屬性
overflow visible, hidden, scroll 對應(yīng) CSS overflow 屬性,但閹割了 auto 取值
elevation<br />Android <number> CSS中沒有對應(yīng)的屬性,只在 Android5.0+ 上有效
resizeMode cover, contain, stretch CSS中沒有對應(yīng)的屬性,可以參考 background-size 屬性
overlayColor<br />Android string CSS中沒有對應(yīng)的屬性,當(dāng)圖像有圓角時,將角落都充滿一種顏色
tintColor<br />iOS <color> CSS中沒有對應(yīng)的屬性,iOS 圖像上特殊的色彩,改變不透明像素的顏色

<a name="values"></a>

Valuse 取值

<a name="color"></a>

Color 顏色

React Native 支持了 CSS 中大部分的顏色類型:

  • #f00 (#rgb)
  • #f00c (#rgba):CSS 中無對應(yīng)的值
  • #ff0000 (#rrggbb)
  • #ff0000cc (#rrggbbaa):CSS 中無對應(yīng)的值
  • rgb(255, 0, 0)
  • rgba(255, 0, 0, 0.9)
  • hsl(360, 100%, 100%)
  • hsla(360, 100%, 100%, 0.9)
  • transparent
  • 0xff00ff00 (0xrrggbbaa):CSS 中無對應(yīng)的值
  • Color Name:支持了 基本顏色關(guān)鍵字拓展顏色關(guān)鍵字,但不支持 28個系統(tǒng)顏色

<a name="number"></a>

Number 數(shù)值

React-Native 中,目前僅支持 Number 這一種長度取值。默認缺省了 pt 單位,詳細請看 Units 單位 部分。

<a name="units"></a>

Units 單位

<a name="pt"></a>

Pt 點

React-Native 中,并不支持百分比單位,目前只支持一種單位,即 pt 絕對長度單位,同時,你在定義時不需要加單位,例如:

<View style={{width: 100, height: 50}}></View>
var styles = StyleSheet.create({
    box: {
        width: 100,
        height: 50
    }
});

<a name="pixelratio"></a>

PixelRatio 像素密度

React-Native 中,訪問設(shè)備像素密度的方法由 PixelRatio 類提供。

我們的應(yīng)用通常都會運行在不同的設(shè)備上,并且這些設(shè)備的像素密度很有可能各不相同。這會造成一個現(xiàn)象就是:

  • 定義了元素的邊框為 1像素,而實際上在不同像素密度的設(shè)備上結(jié)果不一樣,比如:iPhone6 顯示為 2像素,iPhone6 Plus 顯示為 3像素;
  • 對于一個圖片來講,因為設(shè)備的像素密度不同,它也需要對應(yīng)不同尺寸的規(guī)則,以防止圖片過小變得模糊;

根據(jù)像素密度指定邊框厚度

出于對產(chǎn)品體驗的一致性,我們會要求不論是在哪種設(shè)備上,其邊框厚度都應(yīng)該是相同的。一個取得物理上的相同邊框厚度的好方法就是用邏輯尺寸除以像素密度比:

var styles = StyleSheet.create({
    box: {
        borderWidth: 1 / PixelRatio.get(),
        borderStyle: solid
    }
});

上述代碼將保證你的應(yīng)用在所有的設(shè)備上(像素密度),都獲得 1像素 的邊框厚度。PixelRatio 通過 get() 方法來返回設(shè)備的像素密度。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,733評論 25 709
  • 本文出自《React Native學(xué)習(xí)筆記》系列文章。 一款好的APP離不了一個漂亮的布局,本文章將向大家分享Re...
    CrazyCodeBoy閱讀 37,644評論 3 278
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J的外補...
    _Yfling閱讀 14,090評論 1 92
  • 任何事情都有解決的辦法 付出總會有回報 你說的內(nèi)容反映的是思想 生活都很不容易,也無須處處刁難自己,但是,在此之前...
    橘子潘閱讀 337評論 0 1
  • 民以食為天,吃飯是人生的第一件大事,人一輩子不就是為了一碗飯在奮斗嗎。只不過以前的人只要吃飽就行,而現(xiàn)在的人追求地...
    北風(fēng)吹來閱讀 764評論 0 0

友情鏈接更多精彩內(nèi)容