1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】Twitterのような公式マークを作成する

Posted at

はじめに

公式マークを作る機会があったので記録しておきます。

simulator_screenshot_9584F1E0-24D0-4203-9342-55C2D3180225.png simulator_screenshot_B7A4408A-A7C0-4594-91D9-A351451986FB.png simulator_screenshot_AD70DD23-3E08-472C-B432-84EC3B1C7A4F.png simulator_screenshot_1EAA55E3-2B90-4407-8A3F-BD57617D8441.png

実装

import SwiftUI

struct OfficialMark: View {
    private let size: CGFloat
    
    private let checkMarkColor: Color
    
    init(_ size: CGFloat, checkMarkColor: Color = .white) {
        self.size = size
        self.checkMarkColor = checkMarkColor
    }
    
    var body: some View {
        ZStack(alignment: .center) {
            Image(systemName: "seal.fill")
                .resizable()
                .aspectRatio(1.0, contentMode: .fit)
                .frame(width: size, height: size)
                .fontWeight(.black)
            
            Image(systemName: "checkmark")
                .resizable()
                .aspectRatio(1.0, contentMode: .fit)
                .frame(width: size * 0.4, height: size * 0.4)
                .fontWeight(.black)
                .foregroundColor(checkMarkColor)
        }
    }
}

注意
iOS16以下の場合は
.fontWeight(.black)
の代わりに
.font(.system(size: .infinity, weight: .black))
が使えます!

使い方

struct ContentView: View {
    var body: some View {
        OfficialMark(100)
    }
}

Simulator Screenshot - iPhone 14 - 2023-05-25 at 15.39.54.png

サイズを変更する

struct ContentView: View {
    var body: some View {
        OfficialMark(50)
    }
}

Simulator Screenshot - iPhone 14 - 2023-05-25 at 15.43.08.png

背景色を変える

struct ContentView: View {
    var body: some View {
        OfficialMark(100)
            .foregroundColor(.cyan)
    }
}

Simulator Screenshot - iPhone 14 - 2023-05-25 at 15.40.46.png

チェックマークの色を変える

struct ContentView: View {
    var body: some View {
        OfficialMark(100, checkMarkColor: .yellow)
    }
}

Simulator Screenshot - iPhone 14 - 2023-05-25 at 15.42.14.png

おわり

結構再現度高いのでは??

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?