Help us understand the problem. What is going on with this article?

激安NSAttributedString

More than 3 years have passed since last update.

激安感を出したい時にお使いください。

この記事は
✨安い!!お得!!激安CSS!!✨をiOSでNSAttributedStringを使って実装したものです。

上記記事で使われているPoppinsはiOSのフォントに無かったため、似ているフォントを使用しました。
まるっきり同じではありませんが、だいたい同じ感じで実装できました。

スクリーンショット 2017-02-23 23.43.37.png

import UIKit

class ViewController: UIViewController {

    @IBOutlet private weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupLabel()
    }


    private func setupLabel() {
        let attributeText = NSMutableAttributedString(string: "19,800円")
        let textSize = attributeText.string.characters.count

        attributeText.addAttributes(baseAttributes, range: NSRange(location: 0, length: textSize))
        attributeText.addAttributes(priceLargeAttributes, range: NSRange(location: 0, length: 3))
        attributeText.addAttributes(priceSpaceAttributes, range: NSRange(location: 5, length: 2))
        attributeText.addAttributes(yenAttributets, range: NSRange(location: 6, length: 1))

        label.attributedText = attributetText
    }

    private var baseAttributes: [String: Any] {
        let shadow = NSShadow()
        shadow.shadowOffset = CGSize(width: 5.0, height: 5.0)
        shadow.shadowBlurRadius = 5.0

        let attributes: [String: Any] = [
            NSForegroundColorAttributeName: #colorLiteral(red: 0.7803921569, green: 0.01960784314, blue: 0.02352941176, alpha: 1),
            NSStrokeColorAttributeName: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1),
            NSStrokeWidthAttributeName: -3.0,
            NSShadowAttributeName: shadow,
            NSFontAttributeName: UIFont(name: "AmericanTypewriter-CondensedBold", size: 40.0)!,
            NSKernAttributeName: -2.0,
            NSObliquenessAttributeName: 0.4, // 文字を斜めにする
        ]

        return attributes
    }

    private var priceLargeAttributes: [String: Any] {
        let attributes: [String: Any] = [
            NSFontAttributeName: UIFont(name: "AmericanTypewriter-CondensedBold", size: 50.0)!,
            ]

        return attributes
    }

    private var priceSpaceAttributes: [String: Any] {
        let attributes: [String: Any] = [
            NSKernAttributeName: 7.0,
            ]

        return attributes
    }

    private var yenAttributes: [String: Any] {
        let attributes: [String: Any] = [
            NSFontAttributeName: UIFont(name: "HiraginoSans-W6", size: 25.0)!,
            NSObliquenessAttributeName: 0.0,
            ]

        return attributes
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした