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

标签 javascript angular typescript ionic2

我找不到将自定义图标添加到 ionic 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();
 }

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

最佳答案

ionic doc似乎没有提出任何 native 方法来执行此操作。 但是,有一种方法可以使用自定义类来实现它。

   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;
}

关于javascript - 将我的自定义图标添加到 ionic 2 actionSheet 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551463/

相关文章:

javascript - 如何添加固定变量名和更改变量名

javascript - 组件的 Angular 2 导入在 VS 中有效,但在浏览器中给出 404

java - 如何将 Angular 4 应用程序与 Spring Boot 堆栈集成?

javascript - 在React中单击MUI模态组件外部

angular - 我需要 Angular 2 中的 app.component.ts 吗?

javascript - 使用 JS/jQuery 将一个 div 中的内容动态附加/添加到另一个 div

javascript - 框架7 : How to refresh a page after changing template data?

javascript - 无法在 Angular js中获取该值

python - 如何将 CSRF token 添加到来自 Django 2.2 的 Angular 8 发布请求

javascript - typescript :从当前类的另一个方法调用方法