Last active
August 22, 2017 08:22
-
-
Save Oni-zerone/82135951fc7e8d69781df5abf94c6284 to your computer and use it in GitHub Desktop.
SOTBubbleLabel
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
// | |
// SOTBubbleLabel.swift | |
// | |
// | |
// Created by Andrea Altea on 18/08/17. | |
// Copyright 2017 Andrea Altea. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable class SOTBubbleLabel: UILabel { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setupUI() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.setupUI() | |
} | |
func setupUI() { | |
self.layoutMargins = UIEdgeInsetsMake(2, 8, 2, 8) | |
} | |
override func draw(_ rect: CGRect) { | |
self.layer.cornerRadius = min(rect.width, rect.height)/2 | |
super.draw(rect) | |
} | |
override func drawText(in rect: CGRect) { | |
let textRect = CGRect(x: rect.origin.x + self.layoutMargins.right, | |
y: rect.origin.y, | |
width: rect.size.width - (self.layoutMargins.left + self.layoutMargins.right), | |
height: rect.size.height) | |
super.drawText(in: textRect) | |
} | |
override var intrinsicContentSize: CGSize { | |
let size = super.intrinsicContentSize | |
return CGSize(width: size.width + self.layoutMargins.left + self.layoutMargins.right, | |
height: size.height + self.layoutMargins.top + self.layoutMargins.bottom) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment