将我的自定义图标添加到ionic 2 actionSheet的按钮

问题描述 投票:3回答:2

我找不到将自定义图标添加到离子2/3 actionSheet的方法。

presentActionSheet() {
   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y' ,
         role: 'destructive',
         icon:"my_icon",
         cssClass:"yoy",
         handler: () => {
           console.log('Destructive clicked');
         }
       },   
       {
         text: 'Cancel',
         role: 'cancel',
         handler: () => {
           console.log('Cancel clicked');
         }
       }
     ],

   });

   actionSheet.present();
 }

如何将自己的自定义图标添加到操作表按钮?

javascript angular typescript ionic2
2个回答
3
投票

ionic doc似乎没有提出任何本地方式来做到这一点。但是,有一种方法可以使用自定义类来实现它。

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y',
         // vvvvv set a custom class that will be used to display the icon
         cssClass: "class_used_to_set_icon",
         handler: () => {
           console.log('Add to Y clicked');
         }
       }
     ],

   });

   actionSheet.present();

然后在你的src / app / app.scss中定义以下类:

.class_used_to_set_icon {
  // I'd suggest to have the icon in src/assets (eg ../assets/icon/favicon.ico)
  background-image: url("path/to/your/icon") !important;
  // to properly position the icon:
  background-repeat: no-repeat !important;
  background-position: 10px 10px !important;
  // to indent the text to the right of the icon:
  padding-left: 70px !important;
}

编辑 使用像icomoon这样的字体的自定义图标的另一种方法:

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y',
         // vvvvv set the custom icon class and a custom class to then improve display
         cssClass: "icon-drink actionSheet_withIcomoon",
         handler: () => {
           console.log('Add to Y clicked');
         }
       }
     ],

   });

   actionSheet.present();

然后在你的src / app / app.scss中定义以下类:

// display the icon at the right size
.actionSheet_withIcomoon { font-size: 2.4rem !important; }
// correct the font dimensions and positioning of the text
.actionSheet_withIcomoon .button-inner {
  font-family: "Roboto", "Helvetica Neue", sans-serif;
  font-weight: bold;
  padding-left: 52px;
  margin-top: -20px;
  font-size: 1.6rem !important;
}

0
投票

事实证明你可以通过引用FontAwesome来解决这个问题 - 请参阅https://codepen.io/ablewhite/pen/LyZNXZ

在您的示例中,这可能是(一旦引用了FontAwesome CSS):

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: '<i class="icon fa fa-plus"></i> Add to Y' ,
         role: 'destructive',
         cssClass: 'yoy',
         handler: () => {
           console.log('Destructive clicked');
         }
       },   
       {
         text: 'Cancel',
         role: 'cancel',
         handler: () => {
           console.log('Cancel clicked');
         }
       }
     ],

   });

如果您需要FontAwesome中没有的图标,请使用与您自己的自定义字体相同的技术。

© www.soinside.com 2019 - 2024. All rights reserved.