Skip to content

Instantly share code, notes, and snippets.

@macneko-ayu
Last active June 3, 2021 01:20
Show Gist options
  • Save macneko-ayu/7d1df3e0067d9d86234daef64c9469ce to your computer and use it in GitHub Desktop.
Save macneko-ayu/7d1df3e0067d9d86234daef64c9469ce to your computer and use it in GitHub Desktop.
「iOSでチラシっぽい価格レイアウトを再現してみた」のコード
//
// ViewController.swift
// Price
//
// Created by macneko on 2018/09/07.
// Copyright © 2018年 macneko. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
self.view.addSubview(label)
label.frame = self.view.frame
label.textAlignment = .center
let fontSize: CGFloat = 80
let font = UIFont.systemFont(ofSize: fontSize)
let attributeText = NSMutableAttributedString(string: "各種¥(税込)280",
attributes: [.font: font, .foregroundColor: UIColor.red])
// 各種
attributeText.addAttributes([.font: UIFont.systemFont(ofSize: fontSize * 0.4),
.baselineOffset: 2],
range: NSRange(location: 0, length: 2))
// ¥
attributeText.addAttributes([.font: UIFont.systemFont(ofSize: fontSize * 0.6),
.kern: -50],
range: NSRange(location: 2, length: 1))
// (税込)
attributeText.addAttributes([.font: UIFont.systemFont(ofSize: fontSize * 0.2),
.baselineOffset: (fontSize * 0.8) - (fontSize * 0.2) - 4],
range: NSRange(location: 3, length: 4))
// ()のベースライン調整
attributeText.addAttributes([.baselineOffset: (fontSize * 0.8) - (fontSize * 0.2) - 3],
range: NSRange(location: 3, length: 1))
attributeText.addAttributes([.baselineOffset: (fontSize * 0.8) - (fontSize * 0.2) - 3],
range: NSRange(location: 6, length: 1))
label.attributedText = attributeText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment