UILabel subclass appearance in Storyboard
You could make it @IBDesignable
, and then implement prepareForInterfaceBuilder
:
@IBDesignable
public class MyUILabel: UILabel {
public override func awakeFromNib() {
super.awakeFromNib()
configureLabel()
}
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
configureLabel()
}
func configureLabel() {
font = UIFont(name: Constants.DefaultFont, size: 40)
}
}
Note, IB didn't like it when I implemented init(coder:)
, so I moved it into awakeFromNib
.
Also note that when you make an @IBDesignable
class, Apple advises that you create a separate target (e.g. "File" - "New" - "Target..." - "Cocoa Touch Framework") for this designable class. For more information, see WWDC 2014 video What’s New in Interface Builder.
Standard text colour by subclassing UILabel for use on a storyboard - (IBOutlet)
That's because in init method no UI objects aren't initialised yet. You need to do it in awakeFromNib method.
-(void)awakeFromNib {
[super awakeFromNib];
[self setTextColor:kColourDefaultAppColourText];
}
Or you can use appearance. Set it in eg. didFinishLaunchingWithOptions method with your subclass:
[[CHLabel appearance] setTextColor:kColourDefaultAppColourText];
What is the white area around the UILabel when it is selected in storyboard?
It's just the Xcode
UI behaviour of Xcode's interface builder, which highlights the selected UI component. It's not just for UILabel
, every UI component has this effect.
UIAppearance overwrites custom textColor on UILabel
What you're experiencing is simply a matter of the exact timing with which the UIAppearance proxy applies its settings to a new UIView. When are we to suppose it does this? It can't possibly do it before init
, because init
is the first thing that happens in the life of the UIView. Thus, the order of events is like this:
override init(frame: CGRect) {
super.init(frame: frame)
setup() // black
}
// and some time later, UIAppearance proxy comes along and sets it to white
So your goal is to call setup
pretty early in the life of the label — and certainly before the user ever has a chance to see it — but not so early that the UIAppearance proxy acts later. Let's move the call to setup
to a bit later in the life of the label:
// some time earlier, UIAppearance proxy sets it to white
override func didMoveToSuperview() {
setup() // black
}
Now we're acting after the appearance proxy has had a chance to act, and so your settings are the last to operate, and they win the day.
We remain in ignorance of how early we could move the call to setup
and still come along after the appearance proxy setting has been obeyed. If you have time, you might like to experiment with that. For example, willMoveToSuperview
is earlier; if you call setup
there (and not in didMoveToSuperview
), does that work? Play around and find out!
UILabel with text of different color in Storyboard
To achieve that in storyboard
, make the label
's text attributed, then select the range and change colors accordingly.
See the image below:
Hope this helps.
UILabel subclass - text cut off in bottom despite label being correct height
Turns out the problem was with
self.lineBreakMode = .ByClipping
changing it to
self.lineBreakMode = .ByCharWrapping
Solved the problem
Custom UILabel not displaying properly in Interface Builder
I think you need to call this:
[super drawRect:rect];
iPhone iOS UILabel how to customize text color for UITableView detail text labels only?
You could trick around this by subclassing the cells in the detail view, then use
[[UILabel appearanceWhenContainedIn:[YOUR_UITableViewCell class], nil]
setTextColor:[UIColor other_colr]];
Related Topics
How to Turn Off Color Literals in Xcode
Cannot Subscript a Value of Type '[String:String]' with an Index of Type 'String'
Refreshing Auth Token with Moya
Is This Safe to Call Wait() of Dispatchsemaphore Several Times at One Time
How to Initialise a New Nsdocument Instance in Swift
Swift: Move Uiimage Partly Along Path
Swift Protocol Defining Class Method Returning Self
How to Send Data from iPhone to Watchkit in Os2 in Swift
Swift - How to Get Text Formatting in a Text Editor Like in the Notes App? Swiftui
Combining Custom Property Wrapper with @Published
How to Have Arview and Arscnview Coexist
Need Help Converting (Cfpropertylistref *)Nsdictionary to Swift
How to Generate an Auth Token Using Jwt for Google Firebase
How to Declare Protocol Property as Private
Convert JSON Anyobject to Int64