Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nubbel/f675113429b5c7429252 to your computer and use it in GitHub Desktop.
Save nubbel/f675113429b5c7429252 to your computer and use it in GitHub Desktop.
//
// 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
}
}
@softlion
Copy link

softlion commented Mar 4, 2024

Line 56 should that be instead : ?

        size.width  += 2*segmentPadding.width * CGFloat(numberOfSegments)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment