Skip to content

Instantly share code, notes, and snippets.

@fmtonakai
Last active August 6, 2023 21:27
Show Gist options
  • Save fmtonakai/4e3f6011a4a85498478047a0e742f3ec to your computer and use it in GitHub Desktop.
Save fmtonakai/4e3f6011a4a85498478047a0e742f3ec to your computer and use it in GitHub Desktop.
AttributedString with String Interpolation
//
// AttributedString.swift
//
// Created by fm.tonakai on 2019/04/08.
//
import UIKit
public struct AttributedString: ExpressibleByStringLiteral, ExpressibleByStringInterpolation, CustomStringConvertible {
public struct StringInterpolation: StringInterpolationProtocol {
public var attributedString: NSMutableAttributedString
public init(literalCapacity: Int, interpolationCount: Int) {
attributedString = NSMutableAttributedString()
}
public func appendLiteral(_ literal: String) {
attributedString.append(NSAttributedString(string: literal))
}
public func appendInterpolation(_ linkText: String? = nil, URL url: URL) {
let text = linkText ?? url.absoluteString
attributedString.append(NSAttributedString(string: text, attributes: [.link: url]))
}
public func appendInterpolation(_ image: UIImage, bounds: CGRect? = nil) {
let textAttachment = NSTextAttachment()
textAttachment.image = image
if let bounds = bounds {
textAttachment.bounds = bounds
}
attributedString.append(NSAttributedString(attachment: textAttachment))
}
func appendInterpolation(_ text: String, attributes: [NSAttributedString.Key: Any]) {
attributedString.append(NSAttributedString(string: text, attributes: attributes))
}
}
public var attributedString: NSAttributedString
public init(stringLiteral value: String) {
attributedString = NSAttributedString(string: value)
}
public init(stringInterpolation: StringInterpolation) {
attributedString = NSAttributedString(attributedString: stringInterpolation.attributedString)
}
public init(attributedString: NSAttributedString) {
self.attributedString = attributedString
}
public var description: String {
return attributedString.description
}
}
public func + (lhs: AttributedString, rhs: AttributedString) -> AttributedString {
let result = NSMutableAttributedString()
result.append(lhs.attributedString)
result.append(rhs.attributedString)
return AttributedString(attributedString: NSAttributedString(attributedString: result))
}
public func += (lhs: inout AttributedString, rhs: AttributedString) {
let result = NSMutableAttributedString()
result.append(lhs.attributedString)
result.append(rhs.attributedString)
lhs.attributedString = NSAttributedString(attributedString: result)
}
import UIKit
import PlaygroundSupport
// add image named "image" in playground
var attrStr: AttributedString = "新規登録は\("こちら", URL: URL(string: "http://example.com/signin")!)\(UIImage(named: "image")!)\n"
attrStr = attrStr + "ログインは\("あちら", URL: URL(string: "http://example.com/login")!)"
print(attrStr)
let label = UILabel()
label.backgroundColor = .white
label.attributedText = attrStr.attributedString
label.numberOfLines = 0
label.sizeToFit()
PlaygroundPage.current.liveView = label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment