Created
June 15, 2015 21:52
-
-
Save nubbel/f675113429b5c7429252 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
// | |
// SegmentedControl.swift | |
// | |
// Created by Dominique d'Argent on 15/06/15. | |
// Copyright © 2015 Dominique d'Argent. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class SegmentedControl : UISegmentedControl { | |
@IBInspectable | |
var segmentPadding: CGSize = CGSizeZero | |
@IBInspectable | |
var titleFontName: String? | |
@IBInspectable | |
var titleFontSize: CGFloat = UIFont.systemFontSize() | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
setup() | |
} | |
#if TARGET_INTERFACE_BUILDER | |
override func prepareForInterfaceBuilder() { | |
setup() | |
} | |
#endif | |
func setup() { | |
var attributes: [NSObject : AnyObject]? | |
if let fontName = titleFontName, font = UIFont(name: fontName, size: titleFontSize) { | |
attributes = [NSFontAttributeName: font] | |
} | |
setTitleTextAttributes(attributes, forState: .Normal) | |
} | |
override func intrinsicContentSize() -> CGSize { | |
var size = super.intrinsicContentSize() | |
if let fontName = titleFontName, let font = UIFont(name: fontName, size: titleFontSize) { | |
size.height = floor(font.lineHeight + 2 * segmentPadding.height) | |
} | |
else { | |
size.height += segmentPadding.height * 2 | |
} | |
size.width += segmentPadding.width * CGFloat(numberOfSegments + 1) | |
return size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 56 should that be instead : ?