华为仓颉鸿蒙HarmonyOS NEXT原生UI基础组件Span

163 阅读3分钟

作为Text组件的子组件,用于显示行内文本的组件。

子组件

构造函数

init(String)

public init(content: String)
参数名参数类型必填默认值描述
contentString-文本内容。

init(CJResource)

public init(content: CJResource)
参数名参数类型必填默认值描述
contentCJResource-文本内容。

init(PluralResource)

public init(content: PluralResource)
参数名参数类型必填默认值描述
contentPluralResource-文本内容。

函数

decoration(TextDecorationType, Color)

public func decoration(decorationType!: TextDecorationType, color!: Color = Color.BLACK): This

设置文本装饰线样式及其颜色。

参数名参数类型必填默认值描述
decorationTypeTextDecorationType-文本装饰线样式。
colorColorColor.BLACK文本装饰线颜色。

decoration(TextDecorationType, UInt32)

public func decoration(decorationType!: TextDecorationType, color!: UInt32): This

设置文本装饰线样式及其颜色。

参数名参数类型必填默认值描述
decorationTypeTextDecorationType-文本装饰线样式。
colorUInt32-文本装饰线颜色。

decoration(TextDecorationType, CJResource)

public func decoration(decorationType!: TextDecorationType, color!: CJResource): This

设置文本装饰线样式及其颜色。

参数名参数类型必填默认值描述
decorationTypeTextDecorationType-文本装饰线样式。
colorCJResource-文本装饰线颜色。

textCase(TextCase)

public func textCase(value: TextCase): This

设置文本大小写。

参数名参数类型必填默认值描述
textCaseTextCaseTextCase.Normal设置文本大小写。

letterSpacing(Length)

public func letterSpacing(value: Length): This

设置文本字符间距。

参数名参数类型必填默认值描述
valueLength0.vp设置文本字符间距。

letterSpacing(Float64)

public func letterSpacing(value: Float64): This

设置文本字符间距。

参数名参数类型必填默认值描述
valueFloat64-设置文本字符间距。

letterSpacing(Int64)

public func letterSpacing(value: Int64): This

设置文本字符间距。

参数名参数类型必填默认值描述
valueInt640.vp设置文本字符间距。

letterSpacing(CJResource)

public func letterSpacing(value: CJResource): This

设置文本字符间距。

参数名参数类型必填默认值描述
valueCJResource0.vp设置文本字符间距。

通用属性/通用事件

通用属性:仅支持通用文本样式,不支持触摸热区设置。

通用事件:仅支持点击事件,由于Span组件无尺寸信息,因此点击事件返回的ClickEvent对象的target属性无效。

示例代码

import ohos.base.*
    import ohos.component.*
    import ohos.state_manage.*
    import ohos.state_macro_manage.*
    import std.collection.*
    
    
    @Entry
    @Component
    class MyView {
        func build() {
            Flex(FlexParams(direction: FlexDirection.Column, alignItems: ItemAlign.Start,
                            justifyContent: FlexAlign.SpaceBetween)) {
                Text("Basic Usage")
                .fontSize(9)
                .fontColor(0xCCCCCC)
                Text() {
                    Span("This is the Span component")
                    .fontSize(12)
                    .textCase(TextCase.Normal)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                }
    
                Text("Text Decoration")
                .fontSize(9)
                .fontColor(0xCCCCCC)
                Text() {
                    Span("I am Underline-span")
                    .decoration(decorationType: TextDecorationType.Underline, color: Color.RED)
                    .fontSize(12)
                }
                Text() {
                    Span("I am LineThrough-span")
                    .decoration(decorationType: TextDecorationType.LineThrough, color: Color.RED)
                    .fontSize(12)
                }
                Text() {
                    Span("I am Overline-span")
                    .decoration(decorationType: TextDecorationType.Overline, color: Color.RED)
                    .fontSize(12)
                }
    
                Text("Text Case").fontSize(9).fontColor(0xCCCCCC)
                Text() {
                    Span("I am Lower-span")
                    .textCase(TextCase.LowerCase)
                    .fontSize(12)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                }
                Text() {
                    Span("I am Upper-span")
                    .textCase(TextCase.UpperCase)
                    .fontSize(12)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                }
                Text() {
                    Span("I am LetterSpacing")
                    .fontSize(20)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                    .letterSpacing(10)
                }
                Text() {
                    Span("I am Span1").fontSize(30)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                    Span("I am Span2").fontSize(30)
                    .decoration(decorationType: TextDecorationType.None, color: Color.RED)
                }
            }
            .width(100.percent)
            .height(250)
            .padding(left: 35, right: 35, top: 35)
        }
    }

图片

如对您有帮助,帮忙点个“在看 、关注” 让更多的人受益~!

技术交流群可加wx“LB-9191” 备注cangjie