设置根据Text设置如下高亮代码之后,文本无法显示了, 显示空白;
import { Constants } from '../constants/LoginConstants';
/**
* Component of privacy statement and help.
*/
@Component
export struct PrivacyStatementView {
protocolString: string = "《用户服务协议》"
privacyString: string = "《隐私政策》"
allItemString: string = "我已阅读并同意"+this.protocolString+"和"+this.privacyString
mutableStyledString: MutableStyledString = new MutableStyledString(this.allItemString, [{
start: this.allItemString.indexOf(this.protocolString), length: this.protocolString.length,
styledKey: StyledStringKey.FONT, styledValue: new TextStyle({ fontColor: Color.Blue })
}, {
start: this.allItemString.indexOf(this.privacyString), length: this.privacyString.length,
styledKey: StyledStringKey.FONT, styledValue: new TextStyle({ fontColor: Color.Blue })
}]);
controller: TextController = new TextController();
aboutToAppear():void {
console.log(this.allItemString);
this.controller.setStyledString(this.mutableStyledString)
}
build() {
Flex({
direction: FlexDirection.Row,
justifyContent: FlexAlign.Start
}) {
Toggle({ type: ToggleType.Checkbox, isOn: false })
.size({ width: 20, height: 20 })
.selectedColor('#007DFF')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn)
})
Text(undefined, { controller: this.controller }).key("mutableStyledString")
// .fontSize(12).fontColor("#FF999999")
// .textAlign(TextAlign.Start)
}
.width(Constants.FULL_SCREEN)
}
}
部分文字实现高亮请参考demo