本文介绍: 设置文本字体粗细number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值字符串形式,例如“400”,以及“bold”、“bolder”、“lighter”、“regular”、“medium”,分别对应FontWeight中相应的枚举值。使用多个字体使用“,”进行分割优先级顺序生效。LoadingProgress组件用于显示加载进展比如应用登录界面,当我们点击登录时候显示的“正在登录”的进度条状态

目录

之——基础组件

杂谈

正文

1.Image

1.0 数据源 


        基础组件使用

        组件(Component)是界面搭建显示最小单位,HarmonyOS ArkUI声明开发范式开发者提供了丰富多样的UI组件我们可以使用这些组件轻松的编写出更加丰富、漂亮的界面

        组件根据功能可以分为以下五大类:基础组件容器组件媒体组件绘制组件、画布组件。其中基础组件是视图层基本组成单元,包括Text、Image、TextInput、Button、LoadingProgress等。

        例如下面这个常用的登录界面就是由这些基础组件组合而成:


        渲染展示图片支持加载本地网络图片

        Image组件用来渲染展示图片,它可以让界面变得更加丰富多彩。只需要给Image组件设置图片地址、宽和高,图片就能加载出来。

Image($r("app.media.icon"))
  .width(100)
  .height(100)

        其中,图片数据源

 

        为了使图片页面中有更好显示效果,有时候需要图片进行缩放处理。可以使用objectFit属性设置图片的缩放类型,objectFit参数类型为ImageFit

//将图片加载到Image组件,设置宽高各100,设置objectFit为Cover默认值),设置图片背景色灰色0xCCCCCC
Image($r("app.media.image2"))
  .objectFit(ImageFit.Cover)
  .backgroundColor(0xCCCCCC)
  .width(100)
  .height(100) 

        ImageFit包含以下几种类型:

        大小调整,float.json

        浏览新闻的时候,图片一般从网络加载而来,Image组件支持加载网络图片,将图片地址换成网络图片地址进行加载。

Image('https://www.example.com/xxx.png')

        为了成功加载网络图片,需要module.json5文件中申明网络访问权限

{
    "module" : {
        "requestPermissions":[
           {
             "name": "ohos.permission.INTERNET"
           }
        ]
    }
}       

        展示文本信息

         其中,文本来源,string..json

         文本大小fontSize,float.json

        文本粗细fontWeight

        设置文本字体粗细number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值字符串形式,例如“400”,以及“bold”、“bolder”、“lighter”、“regular”、“medium”,分别对应FontWeight中相应的枚举值。默认值:FontWeight.Normal

        文本颜色fontColor:

        还有fontStyle设置样式,FontStyle.Italic是斜体;fontFamily设置文本的字体列表。使用多个字体,使用“,”进行分割优先级顺序生效。例如:“Arial,sansserif”。

@Entry
@Component
struct TextDemo {
  build() {
    Row() {
      Column() {
        Text('HarmonyOS')
        Text('HarmonyOS')
          .fontColor(Color.Blue)
          .fontSize(20)
          .fontStyle(FontStyle.Italic)
          .fontWeight(FontWeight.Bold)
          .fontFamily('Arial')
      }
      .width('100%')
    }
    .backgroundColor(0xF1F3F5)
    .height('100%')
  }
}

         文本对齐方式textAlign:

Text('HarmonyOS')
  .width(200)
  .textAlign(TextAlign.Start)
  .backgroundColor(0xE6F2FD)

        TextAlign.Start/Center/End

        文本超长显示,textOverflow:(需配合maxLines使用,单独设置不生效

 

Text('This is the text content of Text Component This is the text content of Text Component')
  .fontSize(16)
  .maxLines(1)
  .textOverflow({overflow:TextOverflow.Ellipsis})
  .backgroundColor(0xE6F2FD) 

        文本装饰线,decoration: 

Text('HarmonyOS')
  .fontSize(20)
  .decoration({ type: TextDecorationType.Underline, color: Color.Black })
  .backgroundColor(0xE6F2FD)

        TextDecorationTyp包含None、Overline、LineThrough、Underline


        输入单行文本,响应输入事件获取用户输入的文本信息

TextInput()
  .fontColor(Color.Blue)
  .fontSize(20)
  .fontStyle(FontStyle.Italic)
  .fontWeight(FontWeight.Bold)
  .fontFamily('Arial') 

        输入类型枚举

        提示文本样式,placeholderFont:

TextInput({ placeholder: '请输入帐号' })
  .placeholderColor(0x999999)
  .placeholderFont({ size: 20, weight: FontWeight.Medium, family: 'cursive', style: FontStyle.Italic })

        光标位置,TextInputController的caretPosition动态设置光标位置

@Entry
@Component
struct TextInputDemo {
  controller: TextInputController = new TextInputController()
 
  build() {
    Column() {
      TextInput({ controller: this.controller })
      Button('设置光标位置')
        .onClick(() => {
          this.controller.caretPosition(2)
        })
    }
    .height('100%')
    .backgroundColor(0xE6F2FD)
  }
}


        响应点击操作

Button('登录', { type: ButtonType.Capsule, stateEffect: true })
  .width('90%')
  .height(40)
  .fontSize(16)
  .fontWeight(FontWeight.Medium)
  .backgroundColor('#007DFF')

         type用于定义按钮样式,示例代码中ButtonType.Capsule表示胶囊形按钮stateEffect用于设置按钮按下是否开启切换效果,当状态置为false时,点击效果关闭默认值true

        Button样式枚举

        Button组件可以包含子组件,以开发出更丰富多样的Button,下面的示例代码中Button组件包含一个Image组件:

Button({ type: ButtonType.Circle, stateEffect: true }) {
  Image($r('app.media.icon_delete'))
    .width(30)
    .height(30)
}
.width(55)
.height(55)
.backgroundColor(0x317aff)


         LoadingProgress组件用于显示加载进展比如应用登录界面,当我们点击登录的时候,显示的“正在登录”的进度条状态。LoadingProgress的使用非常简单,只需要设置颜色和宽高就可以了。

LoadingProgress()
  .color(Color.Blue)
  .height(60)
  .width(60)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注