Last active
July 13, 2018 15:00
-
-
Save popmedic/75916486747f60cd37cdd6cb357210be 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
import UIKit | |
public class FontPickerTextField: PickerTextField { | |
public required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
for familyName in UIFont.familyNames() { | |
self.choices.appendContentsOf(UIFont.fontNamesForFamilyName(familyName)) | |
} | |
self.choices = self.choices.sort() | |
self.text = self.choices[0] | |
self.layer.shadowRadius = 1.0 | |
self.onSelected = self._onSelected | |
} | |
public var onFontSelected:((font:UIFont)->Void)? = nil | |
public func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { | |
return 44.0 | |
} | |
public func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView { | |
if let timer = self._selectedTimer { | |
timer.invalidate() | |
self._selectedTimer = nil | |
} | |
let frame = CGRect(x: 0, y: 0, width: pickerView.frame.size.width, height: 44.0) | |
var aView:UIView | |
if let view = view { | |
aView = view | |
} | |
else { | |
aView = UIView(frame:frame) | |
} | |
let label = UILabel(frame: frame) | |
if let font = UIFont(name: self.choices[row], size: UIFont.systemFontSize()) { | |
label.attributedText = NSAttributedString(string: self.choices[row], attributes: [NSFontAttributeName:font]) | |
} | |
else { | |
label.text = self.choices[row] | |
} | |
label.textAlignment = NSTextAlignment.Center | |
aView.addSubview(label) | |
return aView | |
} | |
@objc private func _onSelected(row: Int) { | |
self.font = UIFont(name: self.choices[row], size: UIFont.systemFontSize()) | |
if let selected = self.onFontSelected { | |
if let font = UIFont(name: self.choices[row], size: UIFont.systemFontSize()) { | |
selected(font: font) | |
} | |
else { | |
selected(font: UIFont()) | |
} | |
} | |
} | |
public var currentFont:UIFont { | |
get { | |
if let font = UIFont(name: self.choices[self.currentRow], size: UIFont.systemFontSize()) { | |
return font | |
} | |
else { | |
return UIFont() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment