ionic2 tabs 圖標(biāo)自定義實(shí)例
一、準(zhǔn)備資源
tabs icon 的svg格式的矢量圖片

二、生成字體樣式文件
打開icoMoon網(wǎng)站去制作字體文件。



三、使用字體文件
解壓下載的文件,將其中的fonts文件夾拷貝到ionic2項(xiàng)目的src/assest目錄下。并且將其目錄下的styles.css文件命名為wecare.scss(這個(gè)名稱是你字體的文件名稱),再將其拷貝到ionic2項(xiàng)目的src/assest/fonts目錄下。下來我們?nèi)バ薷膚ecare.scss文件。
修改@font-face中的引用地址。
$font-path: '../assets/fonts';
@font-face {
font-weight: normal;
font-style: normal;
font-family: 'wecare';
src: url('#{$font-path}/wecare.eot?l80w4l');
src: url('#{$font-path}/wecare.eot?l80w4l#iefix') format('embedded-opentype'),
url('#{$font-path}/wecare.ttf?l80w4l') format('truetype'),
url('#{$font-path}/wecare.woff?l80w4l') format('woff'),
url('#{$font-path}/wecare.svg?l80w4l#wecare') format('svg');
}
修改字體的公共樣式部分的選擇器
原代碼
[class^="ion-"], [class*=" ion-"] {
/* use !important to prevent issues with browser extensions that change fonts */
font-family: 'wecare' !important;
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
/* Better Font Rendering =========== */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
修改后
.wecare {
text-transform: none;
font-weight: normal;
font-style: normal;
font-variant: normal;
font-family: 'wecare' !important;
line-height: 1;
/* Better Font Rendering =========== */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
speak: none;
}
.ion-ios-smart-outline:before,
.ion-ios-smart:before,
.ion-md-smart-outline:before,
.ion-md-smart:before,
.ion-ios-test-outline:before,
.ion-ios-test:before,
.ion-md-test-outline:before,
.ion-md-test:before {
@extend .wecare;
}
注:將一個(gè)通用的選擇器改為一個(gè)類,然后讓被應(yīng)用的類去繼承,是因?yàn)槭褂肹class^="ion-"], [class*=" ion-"] 來選擇時(shí)會(huì)影響到ionic2自帶的圖標(biāo)庫,這也是ionic2字體庫的寫法。
字體的引用
.ion-ios-smart-outline:before {
content: '\e904';
}
.ion-ios-smart:before {
content: '\e905';
}
.ion-md-smart-outline:before {
content: '\e904';
}
.ion-md-smart:before {
content: '\e905';
}
.ion-ios-test-outline:before {
content: '\e900';
}
.ion-ios-test:before {
content: '\e901';
}
.ion-md-test-outline:before {
content: '\e900';
}
.ion-md-test:before {
content: '\e901';
}
注:在ionic2中引用圖標(biāo)主要靠名稱來引用,它會(huì)根據(jù)Platform自動(dòng)添加前綴如ios或android(.ion-ios-smart.ion-md-smart),所以我們必須為ios與android各寫一個(gè)類名,其中像.ion-ios-test-outline這種是tab未被選中時(shí)的樣式。
將我們的sass文件導(dǎo)入src/theme/variables.scss ,然后就可以在tabs中使用了。
@import '../assets/fonts/wecare'; <ion-tabs> <ion-tab [root]="tab1Root" tabTitle="Now" tabIcon="test"></ion-tab> <ion-tab [root]="tab2Root" tabTitle="Assessment" tabIcon="information-circle"></ion-tab> <ion-tab [root]="tab3Root" tabTitle="Trends" tabIcon="contacts"></ion-tab> <ion-tab [root]="tab4Root" tabTitle="Smart" tabIcon="smart"></ion-tab> <ion-tab [root]="tab5Root" tabTitle="Settings" tabIcon="contacts"></ion-tab> </ion-tabs>
四、解決android中icon只能使用一張icon的問題
在ionic2中android的tab圖標(biāo)選中與未選中只能使用一張svg圖片,只是顏色不同。如果我們想要像在ios中一樣選中與未選中使用不同的icon,就不能辦到。我們的項(xiàng)目的需求就是這樣,我不得已只能去看ionic2關(guān)于tabs與tab的源碼,找到解決問題的方法。我們應(yīng)該知道在ios中它是通過-outline的后綴來控制顯示未選中的icon。起初我看了tabs與tab的源碼,沒有找到關(guān)于這個(gè)問題的部分。最后在icon的源碼中找到了關(guān)于這個(gè)問題的部分代碼。
if (iconMode === 'ios' && !this._isActive && name.indexOf('logo-') < 0 && name.indexOf('-outline') < 0) {
name += '-outline';
}
它是判斷是否是ios與 !this._isActive時(shí)給icon的name添加-outline后綴。但是我們也不能去改源碼,因?yàn)槿绻院笊?jí)會(huì)覆蓋。
但是天無絕人之路,在tab的api中提供了一個(gè)被選中時(shí)調(diào)用的方法,我們通過這個(gè)方法來改變icon。下面是代碼
<ion-tabs>
<ion-tab [root]="tab1Root" (ionSelect)="change(0)" tabTitle="Now" tabIcon="{{test[0]}}"></ion-tab>
<ion-tab [root]="tab2Root" (ionSelect)="change(1)" tabTitle="Assessment" tabIcon="information-circle"></ion-tab>
<ion-tab [root]="tab3Root" (ionSelect)="change(2)" tabTitle="Trends" tabIcon="contacts"></ion-tab>
<ion-tab [root]="tab4Root" (ionSelect)="change(3)" tabTitle="Smart" tabIcon="{{test[3]}}"></ion-tab>
<ion-tab [root]="tab5Root" (ionSelect)="change(4)" tabTitle="Settings" tabIcon="contacts"></ion-tab>
</ion-tabs>
test: Array<string> = ["test", "", "", "smart", ""];
change(a: number) {
if (this.platform.is("android")) {
for (let i = 0; i < 5; i++) {
if (i === a) {
this.test[i] = this.test[i].split("-")[0];
} else {
this.test[i] = this.test[i].split("-")[0] + "-outline";
}
}
}
}
五、最后來看一下效果

以上所述是小編給大家介紹的ionic2 tabs 圖標(biāo)自定義實(shí)例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
通過隱藏iframe實(shí)現(xiàn)文件下載的js方法介紹
本篇文章主要是對通過隱藏iframe實(shí)現(xiàn)文件下載的js方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法
這篇文章主要介紹了js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法,涉及javascript針對日期及時(shí)間的相關(guān)操作技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-08-08
js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能
本文主要介紹了js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能的實(shí)例代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
js 數(shù)組當(dāng)前行添加數(shù)據(jù)方法詳解
這篇文章主要介紹了js 數(shù)組當(dāng)前行添加數(shù)據(jù)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
js中.sort()函數(shù)的常見用法與高級(jí)操作
JavaScript中的sort函數(shù)可以用來對數(shù)組進(jìn)行排序,默認(rèn)情況下sort函數(shù)將數(shù)組中的元素轉(zhuǎn)換為字符串,并按照Unicode碼點(diǎn)的順序進(jìn)行排序,下面這篇文章主要給大家介紹了關(guān)于js中.sort()函數(shù)的常見用法與高級(jí)操作的相關(guān)資料,需要的朋友可以參考下2023-05-05
微信小程序教程系列之頁面跳轉(zhuǎn)和參數(shù)傳遞(6)
這篇文章主要為大家詳細(xì)介紹了微信小程序教程系列之頁面跳轉(zhuǎn)和參數(shù)傳遞,微信小程序提供了3種頁面跳轉(zhuǎn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
JS?const關(guān)鍵字生成常量的三個(gè)特點(diǎn)介紹
這篇文章主要介紹了JS?const關(guān)鍵字生成常量有哪些特點(diǎn),下面通過代碼演示const關(guān)鍵字聲明的常量的塊級(jí)作用域效果,需要的朋友可以參考下2023-05-05

