HarmonyOS 鸿蒙Next中文字描边与文字渐变

HarmonyOS 鸿蒙Next中文字描边与文字渐变 1.使用textShadow实现文字外描边

@Entry
@Component
struct Index {
  offsetNumber: number = 3

  build() {
    Row() {
      Column() {
        Row() {
          Text('blendMode')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .blendMode(BlendMode.DST_IN, BlendApplyType.OFFSCREEN)
        }
        .linearGradient({
          direction: GradientDirection.Right,
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
        })
        .blendMode(BlendMode.SRC_OVER, BlendApplyType.OFFSCREEN)


        Text('textShadow')
          .textAlign(TextAlign.Center)
          .fontSize(40)
          .textShadow([
            { radius: 1, color: Color.White, offsetX: -this.offsetNumber, offsetY: 0 },
            { radius: 1, color: Color.White, offsetX: this.offsetNumber, offsetY: 0 },
            { radius: 1, color: Color.White, offsetX: 0, offsetY: this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: 0, offsetY: -this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: -this.offsetNumber, offsetY: -this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: this.offsetNumber, offsetY: this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: -this.offsetNumber, offsetY: this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: this.offsetNumber, offsetY: -this.offsetNumber }
          ])
        Text('textShadow')
          .textAlign(TextAlign.Center)
          .fontSize(40)
          .textShadow([
            { radius: 1, color: Color.White, offsetX: -this.offsetNumber, offsetY: 0 },
            { radius: 1, color: Color.White, offsetX: this.offsetNumber, offsetY: 0 },
            { radius: 1, color: Color.White, offsetX: 0, offsetY: this.offsetNumber },
            { radius: 1, color: Color.White, offsetX: 0, offsetY: -this.offsetNumber }
          ])

        Text('textShadow')
          .textAlign(TextAlign.Center)
          .fontSize(40)
          .textShadow([
            { radius: 3, color: Color.White, offsetX: -this.offsetNumber, offsetY: 0 },
            { radius: 3, color: Color.White, offsetX: this.offsetNumber, offsetY: 0 },
            { radius: 3, color: Color.White, offsetX: 0, offsetY: this.offsetNumber },
            { radius: 3, color: Color.White, offsetX: 0, offsetY: -this.offsetNumber },
          ])

      }.backgroundImage($r("app.media.startIcon"))
      .backgroundImageSize(ImageSize.FILL)
    }
  }
}

2.Canvas实现

// xxx.ets
@Entry
@Component
struct StrokeText {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('rgb(213,213,213)')
        .onReady(() => {
          this.context.font = "14vp"
          this.context.strokeStyle = '#fff'
          this.context.lineWidth = 2
          this.context.fillStyle = '#000'
          this.context.strokeText("Hello World!", 20, 60)
          this.context.fillText("Hello World!", 20, 60)
        })
    }
    .width('100%')
    .height('100%')
  }
}

3.pen绘制效果

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/textblock-drawing-arkts#文字描边

// 创建画笔
let pen = new drawing.Pen();
// 设置抗锯齿
pen.setAntiAlias(true);
// 设置描边线宽
pen.setStrokeWidth(3.0);
// 设置描边颜色
pen.setColor(0xFF, 0xFF,  0x00, 0x00);
// 创建字型对象
const font = new drawing.Font();
// 设置字体大小
font.setSize(100);
// 添加画笔描边效果
canvas.attachPen(pen);
// 创建字块对象
const textBlob = drawing.TextBlob.makeFromString("Hello world", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
// 绘制字块
canvas.drawTextBlob(textBlob, 200, 300);
// 去除描边效果
canvas.detachPen();

更多关于HarmonyOS 鸿蒙Next中文字描边与文字渐变的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

鸿蒙Next中文字描边通过Text组件的textShadow属性实现,可设置阴影偏移、模糊半径和颜色模拟描边效果。文字渐变需使用Text组件的linearGradient或sweepGradient属性,定义渐变方向和色标。具体通过ArkTS声明式语法配置,例如linearGradient可设置角度和颜色数组,实现线性渐变效果。

更多关于HarmonyOS 鸿蒙Next中文字描边与文字渐变的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,文字描边和渐变可以通过多种方式实现,具体取决于你的需求和使用场景。

首先,使用textShadow实现描边是一种常见方法。通过设置多个阴影偏移方向(如上下左右及对角线),可以模拟出描边效果。代码中通过调整offsetXoffsetY的值来控制描边的粗细和方向,radius参数影响模糊程度。这种方式简单直接,适合大多数UI场景。

其次,Canvas提供了更底层的控制。使用strokeTextfillText结合,可以分别绘制描边和填充文字,实现精确的描边效果。通过调整lineWidthstrokeStyle,可以自定义描边颜色和宽度,适合需要复杂图形或动态绘制的场景。

最后,使用Pen绘制是另一种高级选项,适用于需要精细控制文字渲染的情况。通过设置抗锯齿、描边线宽和颜色,再结合TextBlob进行绘制,可以实现高质量的描边效果。这种方法更适合图形密集型应用。

对于文字渐变,可以使用linearGradient,通过指定颜色数组和方向来实现平滑过渡效果。结合blendMode可以进一步调整渐变与背景的混合方式。

总的来说,选择哪种方法取决于你的具体需求:简单UI效果可用textShadow,复杂图形用Canvas,高质量渲染用Pen。渐变则通过linearGradient灵活实现。

回到顶部