How to fix '@IBInspectable' attribute is meaningless on a property that cannot be represented in Objective-C warning
Maybe.
The exact error (not warning) I got when doing a copy/paste of class CircularIndicator: UIView
is:
Property cannot be marked @IBInspectable because its type cannot be
represented in Objective-C
I resolved it by making this change:
@IBInspectable var backgroundIndicatorLineWidth: CGFloat? { // <-- warning here
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
}
}
To:
@IBInspectable var backgroundIndicatorLineWidth: CGFloat = 0.0 {
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth
}
}
Of course, backgroundIndicator
is undefined in my project.
But if you are coding against didSet
, it looks like you just need to define a default value instead of making backgroundIndicatorLineWidth
optional.
Segue from Obj-c to swift can't find property
If you are using Swift file in objc classes Bridging header won't help here.
Xcode will create Header file which you need to import
First goto build settings and search for Defines Modules and set it to true.
Now in .m
file add following header file
#import "YourTargetName-Swift.h"
and build the project
and also add @objc
before your property
Hope it is helpful
Adding space/padding to a UILabel
If you want to stick with UILabel, without subclassing it, Mundi has given you a clear solution.
If alternatively, you would be willing to avoid wrapping the UILabel with a UIView, you could use UITextView to enable the use of UIEdgeInsets (padding) or subclass UILabel to support UIEdgeInsets.
Using a UITextView would only need to provide the insets (Objective-C):
textView.textContainerInset = UIEdgeInsetsMake(10, 0, 10, 0);
Alternative, if you subclass UILabel, an example to this approach would be overriding the drawTextInRect method
(Objective-C)
- (void)drawTextInRect:(CGRect)uiLabelRect {
UIEdgeInsets myLabelInsets = {10, 0, 10, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(uiLabelRect, myLabelInsets)];
}
You could additionally provide your new subclassed UILabel with insets variables for TOP, LEFT, BOTTOM and RIGHT.
An example code could be:
In .h (Objective-C)
float topInset, leftInset,bottomInset, rightInset;
In .m (Objective-C)
- (void)drawTextInRect:(CGRect)uiLabelRect {
[super drawTextInRect:UIEdgeInsetsInsetRect(uiLabelRect, UIEdgeInsetsMake(topInset,leftInset,bottomInset,rightInset))];
}
From what I have seen, it seems you have to override the intrinsicContentSize of the UILabel when subclassing it.
So you should override intrinsicContentSize like:
- (CGSize) intrinsicContentSize {
CGSize intrinsicSuperViewContentSize = [super intrinsicContentSize] ;
intrinsicSuperViewContentSize.height += topInset + bottomInset ;
intrinsicSuperViewContentSize.width += leftInset + rightInset ;
return intrinsicSuperViewContentSize ;
}
And add the following method to edit your insets, instead of editing them individually:
- (void) setContentEdgeInsets:(UIEdgeInsets)edgeInsets {
topInset = edgeInsets.top;
leftInset = edgeInsets.left;
rightInset = edgeInsets.right;
bottomInset = edgeInsets.bottom;
[self invalidateIntrinsicContentSize] ;
}
It will update the size of your UILabel to match the edge insets and cover the multiline necessity you referred to.
After searching a bit I have found this Gist with an IPInsetLabel. If none of those solutions work you could try it out.
There was a similar question (duplicate) about this matter.
For a full list of available solutions, see this answer: UILabel text margin
Related Topics
Why Can't I Divide Integers in Swift
Xcode Swift How to Add Image to Uialertcontroller Options
How to Instantiate a Storyboard from a File Within an iOS Playground
Swift Property Observer in Protocol Extension
Asyncdetached Falling Back into Main Thread After Mainactor Call
How to Change Colour of the Certain Words in Label - Swift 3
Target Parameter in Dispatchqueue
How to Delete from Firebase Database
Understanding Shorthand Closure Syntax for Map Function in Swift
Use 'Self' as a Default Parameter
Can Swift Playgrounds See Other Source Files in the Same Project
Firebase Says That My Rules Are Insecure, Why
Example of Dispatch_Once in Swift
Views Compressed by Other Views in Swiftui VStack and List
How to Use .Svg Images in Swiftui
How to Create String Split Extension with Regex in Swift
Swift 3, Xcode 8 Instantiate View Controller Is Not Working
Nsbundle.Mainbundle().Urlforresource("Bach1", Withextension: "Jpg") Returning Null