@@ -11,8 +11,8 @@ export class AvatarComponent implements OnChanges, OnInit {
11
11
isNobody = true ;
12
12
isErrorImg = false ;
13
13
/**
14
- * 自定义头像显示文字
15
- */
14
+ * 自定义头像显示文字
15
+ */
16
16
@Input ( ) gender : 'male' | 'female' | string ;
17
17
/**
18
18
* avatar宽度
@@ -22,45 +22,46 @@ export class AvatarComponent implements OnChanges, OnInit {
22
22
* avatar高度
23
23
*/
24
24
@Input ( ) height = 36 ;
25
-
26
25
/**
27
26
* 是否是圆形n
28
27
*/
29
28
@Input ( ) isRound = true ;
30
-
31
29
/**
32
30
* 是否是图片
33
31
*/
34
32
@Input ( ) imgSrc : string ;
35
33
/**
36
- * 用户名称
37
- */
34
+ * 用户名称
35
+ */
38
36
@Input ( ) name : string ;
39
37
/**
40
38
* 自定义头像显示文字
41
39
*/
42
40
@Input ( ) customText : string ;
43
-
41
+ /**
42
+ * 头像中间文字最小尺寸
43
+ */
44
+ MINIMUM_FONT_SIZE = 12 ;
44
45
fontSize = 12 ;
45
46
code : number ;
46
47
nameDisplay : string ;
47
- constructor ( ) { }
48
+
48
49
ngOnInit ( ) : void {
49
50
this . calcValues ( this . customText ? this . customText : this . name ) ;
50
51
}
52
+
51
53
ngOnChanges ( changes : SimpleChanges ) : void {
52
- if ( changes [ 'width' ] && ! changes [ 'width' ] . isFirstChange ( ) ||
53
- changes [ 'customText' ] && ! changes [ 'customText' ] . isFirstChange ( ) ||
54
- changes [ 'gender' ] && ! changes [ 'gender' ] . isFirstChange ( ) ||
55
- changes [ 'height' ] && ! changes [ 'height' ] . isFirstChange ( ) ||
56
- changes [ 'name' ] && ! changes [ 'name' ] . isFirstChange ( )
57
- ) {
54
+ const { width, customText, gender, height, name } = changes ;
55
+ const result = [ width , customText , gender , height , name ] . map ( ( item ) => item && ! item . isFirstChange ( ) ) ;
56
+ if ( result . includes ( true ) ) {
58
57
this . calcValues ( this . customText ? this . customText : this . name ) ;
59
58
}
60
59
}
60
+
61
61
showErrAvatar ( ) {
62
62
this . isErrorImg = true ;
63
63
}
64
+
64
65
calcValues ( nameInput ) {
65
66
const userName = nameInput ;
66
67
const minNum = Math . min ( this . width , this . height ) ;
@@ -74,8 +75,10 @@ export class AvatarComponent implements OnChanges, OnInit {
74
75
} else {
75
76
this . isNobody = true ;
76
77
}
77
- this . fontSize = minNum / 4 + 3 ;
78
+ const size = minNum / 4 + 3 ;
79
+ this . fontSize = size < this . MINIMUM_FONT_SIZE ? this . MINIMUM_FONT_SIZE : size ;
78
80
}
81
+
79
82
setDisplayName ( name , width ) {
80
83
if ( this . customText ) {
81
84
this . nameDisplay = this . customText ;
@@ -109,6 +112,7 @@ export class AvatarComponent implements OnChanges, OnInit {
109
112
}
110
113
this . getBackgroundColor ( name . substr ( 0 , 1 ) ) ;
111
114
}
115
+
112
116
getBackgroundColor ( char ) {
113
117
if ( this . gender ) {
114
118
if ( this . gender . toLowerCase ( ) === 'male' ) {
0 commit comments