I wrote a tiny class that inherits from UILabel
import UIKit
class ConnectionStateLabel: UILabel, BTConnectionStateChangedDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
func didConnect() {
self.text = "Connected"
}
func didDisconnect() {
self.text = "Disconnected"
}
}
As far as I know, in Xcode that class should now be selectable from the dropdown menu (section "Custom Class") for any UILabel that I drag into my story board, but it does not show up. What is wrong with that class? What did I miss?
Related
Below is my code for my NSTableview cell
class MyTableCellView: NSTableCellView {
var aTextField: NSTextField?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
aTextField = NSTextField(frame: frameRect)
aTextField?.drawsBackground = false
aTextField?.isBordered = false
self.addSubview(aTextField!)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
How can I enable copy and paste to this cell programatically?
I constantly find "leaks" using Xcode instruments (8.2) where free is the last call on the object/memory. If free is being called successfully then why would instruments be reporting this as a leak? Is this really a leak or a bug in instruments?
Here is one example
For example, this small segment of code seems to cause instruments to report leaks like these.
class RDTranslateComponentView: RDMessageCellComponentView {
let textView: RDTextMessageCellComponentView
let button: UIButton
convenience init() {
self.init(frame: CGRect.zero)
}
override init(frame: CGRect) {
self.textView = RDTextMessageCellComponentView()
self.button = UIButton(type: .custom)
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
self.textView.label.font = UIFont.proximaSemiBold(withSize: 14)
self.textView.label.textColor = UIColor.remindBrand()
self.textView.bubble.backgroundColor = UIColor.white
self.addSubviews([ self.textView, self.button ])
self.button.addTarget(self, action: #selector(pressedButton(_:)), for: .touchUpInside)
}
#objc private func pressedButton(_ sender: UIButton?) {
/// Code that calls into a delegate
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return self.textView.sizeThatFits(size)
}
override func layoutSubviews() {
super.layoutSubviews()
self.textView.frame = self.bounds
self.button.frame = self.bounds
}
}
class RDMessageCellComponentView: UIView {
/// The inside of this is just a bag of constants. Doesn't even touch anything View related so I'm omitting.
}
Here's a stack trace from my code in association to a leak. Keep in mind this isn't the only place this happens. This case props up often though and seems to be exclusive to code that has UILabel instantiated inside the init method of a subview that is added as a subview to another view.
I'm trying to create a custom button in my IOS project. But when I'm creating the button with the my class the background doesn't work in the storyboard. But when I run the program the button works fine.
Here you see my class
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
private func style() {
self.backgroundColor = UIColor(CGColor: "#6eb18c".CGColor)
}
}
You need to override prepareForInterfaceBuilder() and call style() from there too.
Try this:
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
override func prepareForInterfaceBuilder() {
style()
}
private func style() {
self.backgroundColor = UIColor.greenColor()
}
}
For example, I want to subclass UIButton and set it's font to 20.0f by default. I can write something like this:
#IBDesignable
class HCButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
But this does not affect preview in Interface Builder, all custom buttons appear with 15.0f font size by default. Any thoughts?
I have created new IBInspectable as testFonts :
import UIKit
#IBDesignable
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
convenience init() {
self.init(frame:CGRectZero)
self.customInit()
}
override func awakeFromNib() {
super.awakeFromNib()
self.customInit()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.customInit()
}
}
Hope it helps you :)
This is working for me.
I think you must override the init with frame initializer as well to affect that
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
EASIER: The following solution usually works or me
import UIKit
#IBDesignable
class CustomButton: UIButton {
open override func layoutSubviews() {
super.layoutSubviews()
customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
I hope that's what you're looking for.
When I create a new subclass of UITextView in the Xcode 6 Beta, the following code is automatically provided.
import UIKit
class TerminalView: UITextView {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
}
The previous code (completely provided by Xcode with nothing removed) gives the following error.
Must call a designated initializer of the superclass 'UITextView'
As far as I know, the designated for all subclasses of UIView is -initWithFrame: (or in Swift, init(frame:). If this is the case, why does the code provided by Xcode result in an error? I have added no new instance variables to the class, so nothing else has to be initialized yet.
It seems as though the only initializer that works for now is:
super.init(frame: CGRect, textContainer: NSTextContainer?)
which can be called with
super.init(frame: CGRect.zero, textContainer: nil)
This is most likely a bug in the initial beta release and will be fixed in upcoming beta releases.
For 2020:
class SpecialText: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
common()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
common()
}
private func common() {
backgroundColor = .yellow
font = .systemFont(ofSize: 26)
textColor = .green
}
}