Skip to content

Commit

Permalink
[iOS] Support RTL (#180)
Browse files Browse the repository at this point in the history
* Update Image

* Update ImagesLoader

* Update XcodeExport

* Update ImagesLoader

localizedCaseInsensitiveContains

* Update README

* Fix README
  • Loading branch information
alexey1312 authored Jul 14, 2022
1 parent b0d055b commit f2321f1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Table of Contents:
* Supports High contrast colors for Xcode
* Supports SwiftUI and UIKit
* Supports Objective-C
* Supports RTL (only iOS)

> Exporting icons and images works only for Professional/Organisation Figma plan because FigmaExport use *Shareable team libraries*.
Expand Down Expand Up @@ -451,6 +452,8 @@ Custom Stencil templates must have the following names:

If a color, icon or image is unique for iOS or Android platform, it should contains "ios" or "android" word in the description field in the properties. If a color, icon or image is used only by the designer, and it should not be exported, the word "none" should be specified in the description field.

If an icon supports RTL (iOS only), it should contains "rtl" word in the description field in the properties.

**Styles and Components must be published to a Team Library.**

For `figma-export colors`
Expand Down
8 changes: 7 additions & 1 deletion Sources/FigmaExport/Loaders/ImagesLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ final class ImagesLoader {
return nil
}
let (name, idiom) = component.name.parseNameAndIdiom(platform: platform)
return Image(name: name, scale: .all, idiom: idiom, url: url, format: params.format)
let isRTL = component.useRTL()
return Image(name: name, scale: .all, idiom: idiom, url: url, format: params.format, isRTL: isRTL)
}
return ImagePack(name: packName, images: packImages, platform: platform)
}
Expand Down Expand Up @@ -394,4 +395,9 @@ extension Component {

return false
}

public func useRTL() -> Bool {
guard let description = description, !description.isEmpty else { return false }
return description.localizedCaseInsensitiveContains("rtl")
}
}
12 changes: 11 additions & 1 deletion Sources/FigmaExportCore/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ public struct Image: Asset {
public let format: String
public let url: URL
public let idiom: String?
public let isRTL: Bool

public var platform: Platform?

public init(name: String, scale: Scale = .all, platform: Platform? = nil, idiom: String? = nil, url: URL, format: String) {
public init(
name: String,
scale: Scale = .all,
platform: Platform? = nil,
idiom: String? = nil,
url: URL,
format: String,
isRTL: Bool = false
) {
self.name = name
self.scale = scale
self.platform = platform
self.url = url
self.idiom = idiom
self.format = format
self.isRTL = isRTL
}

// MARK: Hashable
Expand Down
30 changes: 26 additions & 4 deletions Sources/XcodeExport/Model/XcodeAssetContents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ enum XcodeAssetIdiom: String, Encodable {

struct XcodeAssetContents: Encodable {
struct Info: Encodable {
let version = 1
let author = "xcode"
let version = 1
}
struct Appearance: Encodable {
let appearance: String
Expand All @@ -34,15 +34,37 @@ struct XcodeAssetContents: Encodable {
let components: Components
}
struct ColorData: Encodable {
let idiom = "universal"
var appearances: [Appearance]?
var color: ColorInfo
let idiom = "universal"
}
struct ImageData: Encodable {
let idiom: XcodeAssetIdiom
var scale: String?
var appearances: [Appearance]?
let filename: String
let idiom: XcodeAssetIdiom
let languageDirection: String?
var scale: String?

enum CodingKeys: String, CodingKey {
case appearances
case filename
case idiom
case languageDirection = "language-direction"
case scale
}

init(appearances: [Appearance]?,
filename: String,
idiom: XcodeAssetIdiom,
isRTL: Bool,
scale: String?
) {
self.appearances = appearances
self.filename = filename
self.idiom = idiom
self.languageDirection = isRTL ? "left-to-right" : nil
self.scale = scale
}
}

struct Properties: Encodable {
Expand Down
11 changes: 6 additions & 5 deletions Sources/XcodeExport/Model/XcodeExportExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension ImagePack {
}

func makeXcodeAssetContentsImageData(appearance: Appearance? = nil) -> [XcodeAssetContents.ImageData] {
images.map { $0.makeXcodeAssetContentsImageData(scale: $0.scale, appearance: appearance) }
images.map { $0.makeXcodeAssetContentsImageData(scale: $0.scale, appearance: appearance, isRTL: $0.isRTL) }
}

}
Expand Down Expand Up @@ -155,17 +155,18 @@ extension Image {
return FileContents(destination: destination, sourceURL: url)
}

func makeXcodeAssetContentsImageData(scale: Scale, appearance: Appearance? = nil) -> XcodeAssetContents.ImageData {
func makeXcodeAssetContentsImageData(scale: Scale, appearance: Appearance? = nil, isRTL: Bool) -> XcodeAssetContents.ImageData {
let filename = makeFileURL(scale: scale, appearance: appearance).absoluteString
let xcodeIdiom = idiom.flatMap { XcodeAssetIdiom(rawValue: $0) } ?? .universal
let appearances = appearance.flatMap { $0 == .dark ? [XcodeAssetContents.Appearance.dark] : nil }
let scaleString = scale.string

return XcodeAssetContents.ImageData(
idiom: xcodeIdiom,
scale: scaleString,
appearances: appearances,
filename: filename
filename: filename,
idiom: xcodeIdiom,
isRTL: isRTL,
scale: scaleString
)
}

Expand Down

0 comments on commit f2321f1

Please sign in to comment.