Reputation: 2441
I have created UILabel subclass which is IB_DESIGNABLE and has few IBInspectable properties.
I can see attributed text in all iPhone simulators and in interface builder.
I can see the text in iPhone 5c running iOS 9.3.
BUT no text is displayed for iPhone 6 series of devices which are also running iOS 9.3.
To debug I set background colour to the custom label, I can see the label frame filled with given colour but not text is displayed.
I even try commenting applyStlyeOnText: method, but even after that no text is displayed on iPhone 6.
I am using custom fonts, font file is added to project correctly.
Update: Just updated to Xcode 8.0, on iOS 10 simulators, MyLabel is displaying nothing.
Below is the implementation:
Header File:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface MyLabel : UILabel
@property (nonatomic) IBInspectable CGFloat letterSpacing;
@property (nonatomic) IBInspectable CGFloat lineSpacing;
@end
Implementation File:
#import "MyLabel.h"
@implementation MyLabel
-(void)setLetterSpacing:(CGFloat)letterSpacing
{
if (_letterSpacing != letterSpacing) {
_letterSpacing = letterSpacing;
[self applyStlyeOnText:self.text];
}
}
-(void)setLineSpacing:(CGFloat)lineSpacing
{
if (_lineSpacing != lineSpacing) {
_lineSpacing = lineSpacing;
[self applyStlyeOnText:self.text];
}
}
-(void)applyStlyeOnText:(NSString *)text
{
if (text.length){
NSMutableDictionary * attributes = [NSMutableDictionary new];
if (self.lineSpacing > 0)
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.paragraphSpacing = self.lineSpacing;
paragraphStyle.lineBreakMode = self.lineBreakMode;
paragraphStyle.alignment = self.textAlignment;
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
}
if (self.letterSpacing > 0) {
[attributes setObject:[NSNumber numberWithFloat:self.letterSpacing] forKey:NSKernAttributeName];
}
[attributes setObject:self.font forKey:NSFontAttributeName];
[attributes setObject:self.textColor forKey:NSForegroundColorAttributeName];
[attributes setObject:[UIColor greenColor] forKey:NSBackgroundColorAttributeName];
self.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
NSLog(@"MyLabel - Stlye applied on text : %@", text);
NSLog(@"Thread : %@", [NSThread currentThread]);
[self setBackgroundColor:[UIColor yellowColor]];
}
else
{
//no text recived to apply style on.
self.attributedText = nil;
NSLog(@"MyLabel - Stlye applied on text : empty string received");
}
}
-(void)setText:(NSString *)text
{
[super setText:text];
NSLog(@"MyLabel - set text called with text : %@", text);
NSLog(@"Thread : %@", [NSThread currentThread]);
if (self.lineSpacing > 0 || self.letterSpacing > 0)
{
[self applyStlyeOnText:text];
}
}
}
Upvotes: 0
Views: 818
Reputation: 2441
To debug, I created a new single view app and added MyLabel.h and MyLabel.m files to project.
I Used Xcode 8 and iPhone 5S simulator.
But the result were same no text was being displayed by MyLabel.
Frustrated me, removed all the code from MyLabel.h and MyLabel.m except properties in MyLabel.h, overall class looked like below:
Header File:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface MyLabel : UILabel
@property (nonatomic) IBInspectable CGFloat letterSpacing;
@property (nonatomic) IBInspectable CGFloat lineSpacing;
@end
Implementation File:
#import "MyLabel.h"
@implementation MyLabel
@end
Even with above code, no text was displayed by MyLabel
.
Now I decided to rename the properties like below:
@property (nonatomic) IBInspectable CGFloat ltrSpacing;
@property (nonatomic) IBInspectable CGFloat lnSpacing;
Surprisingly MyLabel
started displaying the text, weird.
Then I re-introduced my logic and everything is working fine.
I think the property names I chose were conflicting with some of UILabel
's private properties but I am not sure about the exact reason.
Upvotes: 1