【发布时间】:2021-06-24 12:29:45
【问题描述】:
我有一个大字体的标签,默认的行高对我来说很宽松。我想将其减少到低于默认值。
提供大于font-size 的line-height 值确实会增加行间距,但较小的值(或负值)不会将其减小到小于iOS 上的默认值。
从a GitHub issue,我得到了这个我更新的sn-p,可以使用最新的NS;
import { Label } from "@nativescript/core/ui/label";
export function setIOSLineHeight(label: Label, lineHeight: number){
const iosLabel = label.nativeView;
let attributedString;
if (iosLabel.attributedText) {
attributedString = iosLabel.attributedText;
} else {
attributedString = NSMutableAttributedString.alloc().initWithString(iosLabel.text);
}
let range = new NSRange({ location: 0, length: iosLabel.text.length });
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
paragraphStyle.lineSpacing = 0;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
attributedString.addAttributeValueRange(NSParagraphStyleAttributeName, paragraphStyle, range);
iosLabel.attributedText = attributedString;
}
但是,在 mounted() 生命周期方法中调用此方法不会对 lineHeight 的任何值产生任何影响 - 即使是通过 CSS 属性产生影响的值:
<template>
<Page ref="page">
<Label ref="topLine" text="Hello this is a text that flows onto multiple lines" textWrap="true" />
</Page>
</template>
<script>
import { isIOS } from 'tns-core-modules/platform';
import { setIOSLineHeight } from '../label-helper.ts';
export default {
mounted() {
if (isIOS) {
/* Has no effect, regardless of value */
setIOSLineHeight(this.$refs.topLine, 40);
}
}
}
</script>
<style lang="scss" scoped>
Label {
font-size: 60;
/* Does work */
line-height: 100;
/* Does not work */
line-height: 40;
}
</style>
如何将Label 的行高减小到小于字体大小的值?
【问题讨论】:
标签: nativescript nativescript-vue