2017-06-18 95 views
0

在响应文本属性下的Ionic 3的CSS实用工具部分,我找不到任何关于此的信息。如何为Ionic Framework中动态更新的不同屏幕尺寸设置不同的字体大小?

我了解到,我们可以针对特定的屏幕尺寸居中对齐文本:

<div [attr.text-center]="isMD ? '' : null">I will be centered when isMD is true.</div> 

但我们不能ngClass使用isMD如因某种原因isMD evaluates to true无论屏幕大小。

<div [ngClass]="{'font-lg': isMD}">I'm visible on all screens.</div> 

我们也有Platform可在我们的离子的应用程序,所以我可以做这样的事情

我想:

screenIsSM: boolean = this.platform.width() <= 576; 
screenIsMD: boolean = this.platform.width() > 576 && this.platform.width() <= 768; 
screenIsLG: boolean = this.platform.width() > 768 && this.platform.width() <= 992; 
screenIsXL: boolean = this.platform.width() > 992 && this.platform.width() <= 1200; 

,并试图用它作为:

<span [ngClass]="{ 
    'font-sm' : screenIsSM, 
    'font-md' : screenIsMD, 
    'font-lg' : screenIsLG, 
    'font-xl' : screenIsXL 
}">Test</span> 

由于某种原因没有任何反应但是,如果我做

{{ screenIsSM ? 'SMALL SCREEN' : 'BIGGER SCREENS' }} 

它不给我想要的结果,但是这似乎在页面加载时,工作只有一次,不会在屏幕尺寸动态变化做出反应。什么是正确的方法?

+0

难道你的屏幕大小从来没有一个设备上更改?屏幕尺寸应该是恒定的。 – wilsonhobbs

+0

我明白这通常是这种情况,但我正在使用Ionic构建PC应用​​程序,该应用程序在所有分辨率和设备中都应该可读。 – anonym

回答

0

你可以在你的构造函数中使用resize事件,所以:

constructor(
    ngZone: NgZone 
) { 
    window.onresize = (e) => 
    { 
     ngZone.run(() => { 
      this.resize(); 
     }); 
    }; 
} 

resize() { 
    screenIsSM: boolean = this.platform.width() <= 576; 
    screenIsMD: boolean = this.platform.width() > 576 && this.platform.width() <= 768; 
    screenIsLG: boolean = this.platform.width() > 768 && this.platform.width() <= 992; 
    screenIsXL: boolean = this.platform.width() > 992 && this.platform.width() <= 1200; 
} 
+0

似乎它应该工作,但它不。无论屏幕大小如何,字体都显示相同。没有课程被应用。 – anonym

+0

而不是platform.width尝试使用本地JS的window.width – wilsonhobbs

相关问题