view2.0暂不支持使用自定义图标,只能通过修改源码去实现 实现后的效果如下
1、先去图标库选择想要的icon加入项目,再生成代码
2、再点击下载代码,解压后如下
3、编写test.js文件,并通过 node test.js 执行
let path1 = "./iconfont.json";
let fs = require('fs');
const path = require('path');
let plays = JSON.parse(fs.readFileSync(path1));
const obj = {}
plays.glyphs.forEach((item)=>{
obj['uicon-'+item.font_class] = '\\'+'u'+item.unicode
})
console.log(obj);
let json = "export default" + JSON.stringify(obj)
json = json.replace(/\\\\/g,'\\')
const r = path.resolve(__dirname,'myicons.js');
fs.writeFile(r, json, 'utf8', (err) => {
if (err) {
console.log('写入文件出错拉!具体错误:' + err)
return message.error("写入路径失败,请重新输入!")
} else {
console.log('ok');
}
});
4、生成myicons.js文件
export default{"uicon-home":"\ue6da","uicon-message":"\ue638","uicon-upcoming":"\ue639","uicon-my":"\ue608"}
5、找到/uni_modules/uview-ui/components/u-icon的文件夹
把myicons.js复制到同目录下
6、修改u-icon.vue文件
引入生成的js文件及iconfont在线字体文件,修改获取icon的方法
<template>
<view class="u-icon" @tap="clickHandler" :class="['u-icon--' + labelPos]">
<image class="u-icon__img" v-if="isImg" :src="name" :mode="imgMode"
:style="[imgStyle, $u.addStyle(customStyle)]"></image>
<text v-else class="u-icon__icon" :class="uClasses" :style="[iconStyle, $u.addStyle(customStyle)]"
:hover-class="hoverClass">{{icon}}</text>
<!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
<text v-if="label !== ''" class="u-icon__label" :style="{
color: labelColor,
fontSize: $u.addUnit(labelSize),
marginLeft: labelPos == 'right' ? $u.addUnit(space) : 0,
marginTop: labelPos == 'bottom' ? $u.addUnit(space) : 0,
marginRight: labelPos == 'left' ? $u.addUnit(space) : 0,
marginBottom: labelPos == 'top' ? $u.addUnit(space) : 0,
}">{{ label }}</text>
</view>
</template>
<script>
const fontUrl = 'https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf'
const domModule = weex.requireModule('dom')
domModule.addRule('fontFace', {
'fontFamily': "uicon-iconfont",
'src': `url('${fontUrl}')`
})
const myFontUrl = 'https://at.alicdn.com/t/font_3396588_o4th3i91nj.ttf?t=1652412137363'
const myDomModule = weex.requireModule('dom')
myDomModule.addRule('fontFace', {
'fontFamily': "my-uicon-iconfont",
'src': `url('${myFontUrl}')`
})
import icons from './icons'
import myicons from './myicons.js'
import props from './props.js';;
export default {
name: 'u-icon',
data() {
return {
}
},
mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
computed: {
uClasses() {
let classes = []
classes.push(this.customPrefix + '-' + this.name)
if (this.color && uni.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
classes = classes.join(' ')
return classes
},
iconStyle() {
let style = {}
style = {
fontSize: uni.$u.addUnit(this.size),
lineHeight: uni.$u.addUnit(this.size),
fontWeight: this.bold ? 'bold' : 'normal',
top: uni.$u.addUnit(this.top)
}
if (this.color && !uni.$u.config.type.includes(this.color)) style.color = this.color
return style
},
isImg() {
return this.name.indexOf('/') !== -1
},
imgStyle() {
let style = {}
style.width = this.width ? uni.$u.addUnit(this.width) : uni.$u.addUnit(this.size)
style.height = this.height ? uni.$u.addUnit(this.height) : uni.$u.addUnit(this.size)
return style
},
icon() {
if (this.myIconObj) {
return myicons['uicon-' + this.name] || this.name
} else {
return icons['uicon-' + this.name] || this.name
}
}
},
methods: {
clickHandler(e) {
this.$emit('click', this.index)
this.stop && this.preventEvent(e)
}
}
}
</script>
<style lang="scss" scoped>
@import "../../libs/css/components.scss";
$u-icon-primary: $u-primary !default;
$u-icon-success: $u-success !default;
$u-icon-info: $u-info !default;
$u-icon-warning: $u-warning !default;
$u-icon-error: $u-error !default;
$u-icon-label-line-height:1 !default;
@font-face {
font-family: 'uicon-iconfont';
src: url('https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf') format('truetype');
}
@font-face {
font-family: 'my-uicon-iconfont';
src: url('https://at.alicdn.com/t/font_3396588_o4th3i91nj.ttf?t=1652412137363') format('truetype');
}
.u-icon {
display: flex;
align-items: center;
&--left {
flex-direction: row-reverse;
align-items: center;
}
&--right {
flex-direction: row;
align-items: center;
}
&--top {
flex-direction: column-reverse;
justify-content: center;
}
&--bottom {
flex-direction: column;
justify-content: center;
}
&__icon {
font-family: "my-uicon-iconfont", "uicon-iconfont";
position: relative;
@include flex;
align-items: center;
&--primary {
color: $u-icon-primary;
}
&--success {
color: $u-icon-success;
}
&--error {
color: $u-icon-error;
}
&--warning {
color: $u-icon-warning;
}
&--info {
color: $u-icon-info;
}
}
&__img {
height: auto;
will-change: transform;
}
&__label {
line-height: $u-icon-label-line-height;
}
}
</style>
7、props.js文件增加自定义标识
myIconObj: {
type: Boolean,
default: false
}
8、在方法中使用
<u-tabbar-item text="首页" @click="clickTab">
<u-icon slot="active-icon" :myIconObj="true" name="home" size="24" :color="bgColor" />
</u-tabbar-item>
|