Last active
March 3, 2017 17:11
-
-
Save OscarSwanros/b4f7bab196a2057faa951d497adc19ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Test | |
// | |
// Created by Oscar Swanros on 3/3/17. | |
// Copyright © 2017 Pacific3. All rights reserved. | |
// | |
import UIKit | |
class BackgroundedLabel: UIView { | |
private lazy var internalLabel: UILabel = { | |
let l = UILabel() | |
l.translatesAutoresizingMaskIntoConstraints = false | |
l.lineBreakMode = .byWordWrapping | |
l.numberOfLines = 0 | |
return l | |
}() | |
override var intrinsicContentSize: CGSize { | |
return CGSize(width: 20, height: 50) | |
} | |
var text: String | |
init(text: String) { | |
self.text = text | |
super.init(frame: .zero) | |
commonInit() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func commonInit() { | |
addSubview(internalLabel) | |
translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate( | |
[ | |
internalLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), | |
internalLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor), | |
internalLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor), | |
internalLabel.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor) | |
] | |
) | |
backgroundColor = .clear | |
let paragraph = NSMutableParagraphStyle() | |
paragraph.lineSpacing = 5 | |
let stringWithAttachment = NSMutableAttributedString() | |
for word in text.components(separatedBy: " ") { | |
let word = image(from: (word as NSString)) | |
let att = NSTextAttachment() | |
att.image = word | |
let space = image(from: " ") | |
let sp = NSTextAttachment() | |
sp.image = space | |
stringWithAttachment.append(NSAttributedString(attachment: att)) | |
stringWithAttachment.append(NSAttributedString(attachment: sp)) | |
} | |
stringWithAttachment.addAttribute(NSParagraphStyleAttributeName, value: paragraph, range: NSRange(location: 0, length: stringWithAttachment.string.characters.count)) | |
internalLabel.attributedText = stringWithAttachment | |
} | |
private func image(from word: NSString) -> UIImage { | |
let attrs = [ | |
NSBackgroundColorAttributeName: UIColor.red, | |
NSFontAttributeName: UIFont.systemFont(ofSize: 18, weight: UIFontWeightRegular), | |
NSForegroundColorAttributeName: UIColor.white | |
] | |
let size = word.size(attributes: attrs) | |
let renderer = UIGraphicsImageRenderer(size: size) | |
return renderer.image { context in | |
UIColor.red.setFill() | |
word.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height), withAttributes: attrs) | |
} | |
} | |
} | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let label = BackgroundedLabel(text: "UPGRADE TO SEE WHO'S BEEN LOOKING AT YOUR PROFILE") | |
view.addSubview(label) | |
label.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 50).isActive = true | |
label.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true | |
label.widthAnchor.constraint(equalToConstant: 300).isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment