Contents

ImageFill

The object for adding an image background fill to a component.

Declaration

object ImageFill

Properties

NameTypeDescription
URL Requireduri

The URL of the image file to use for filling the component.

Image URLs can begin with http://, https://, or bundle://. If the image URL begins with bundle://, the referenced image file must be in the same directory as the document.

Encode image filenames as URLs.

See Preparing Image, Video, Audio, Music, and ARKit Assets.

attachmentstring

A string that indicates how the fill behaves when a person scrolls.

Valid values:

  • scroll (default). The fill scrolls along with its component.

  • fixed. The fill stays at a fixed position within the viewport.

fillModestring

Indicates how Apple News displays the image fill.

Valid values:

  • fit: Scales the image by aspect ratio to fit the component.

  • cover:(default): Scales the image by aspect ratio to completely fill the component.

horizontalAlignmentstring

Sets the horizontal alignment of the image fill within its component.

Valid values:

  • left: Aligns the left edge of the fill with the left edge of the component.

  • center (default): Aligns the horizontal center of the fill with the center of the component.

  • right: Aligns the right edge of the fill with the right edge of the component.

You can use fillMode with horizontalAlignment to achieve the effect you want. For example, set fillMode to fit and horizontalAlignment to left to fit the image based on its aspect ratio and also align the left edge of the fill with the left edge of the component. Or set fillMode to cover and horizontalAlignment to right to scale the image horizontally and also align the right edge of the fill with the right edge of the component.

type Requiredstring

The type of fill to apply. Always set this property to image.

verticalAlignmentstring

Sets the vertical alignment of the image fill within its component.

Valid values:

  • top: Aligns the top of the fill with the top edge of the component.

  • center (default): Aligns the vertical center of the fill with the center of the component.

  • bottom: Aligns the bottom of the fill with the bottom edge of the component.

You can use fillMode with verticalAlignment to achieve the effect you want. For example, set fillMode to fit and verticalAlignment to top to fit the image based on its aspect ratio and also align the top of the fill with the top edge of the component. Or set fillMode to cover and verticalAlignment to top to scale the image vertically and also align the top of the fill with the top edge of the component.

Mentioned in

Discussion

Use the ImageFill object to fill a component with a background image.

To avoid having the image cut off on different devices, calculate the minimum height needed for the image. For example, the following shows the minimum height calculation of a full display based on an image with dimensions 2184 x 1456 pixels:

100 * (height / width) + “cw”

100 * (1456 / 2184) + “cw”

“66.67cw”

For more information about the use of cw, see Specifying Measurements for Components.

You can use this object in ComponentStyle.

Create a Layered Effect with a Background

You can create a layered or collage effect by using a background image with a header container component. Create a header that contains the other components you want in the top section of your article, such as the title and intro, and then apply a background image to the header component. All content in the header component appears in front of the background, creating a layered effect.

Set a Fixed Image Fill

One of the most captivating effects in Apple News Format is the fixed image fill. With this background, the image stays still as a person scrolls, with the rest of the content in the article seeming to move independently of the image. As long as any part of the component containing the image is visible in the viewport, the image remains still.

To create this effect, use a ComponentStyle object with image as the fill type, and then give the attachment property the value of fixed.

[Image]

Example

{
  "components": [
    {
      "role": "container",
      "style": "exampleComponentStyle",
      "layout": {
        "minimumHeight": 150
      },
      "components": [
        {
          "role": "title",
          "text": "Drought",
          "anchor": {
            "targetAnchorPosition": "center"
          },
          "textStyle": {
            "fontSize": 60
          }
        }
      ]
    }
  ],
  "componentStyles": {
    "exampleComponentStyle": {
      "fill": {
        "type": "image",
        "URL": "bundle://image.jpg",
        "fillMode": "cover",
        "verticalAlignment": "top"
      }
    }
  }
}

See Also

Related Documentation

Backgrounds for Components