自定义UILabel不显示文本

问题描述:

我已经创建了UILabel子类,它是IB_DESIGNABLE并且具有很少的IBInspectable属性。自定义UILabel不显示文本

我可以看到所有的iPhone模拟器,并在界面生成器属性文本。

我可以看到运行iOS 9.3的iPhone 5c中的文本。

但没有文本被显示为iPhone 6系列,其也运行iOS 9.3设备。

要调试我设置背景颜色的自定义标签,我可以看到充满给定的颜色的标签框架,但没有显示文本。

我甚至尝试注释applyStlyeOnText:的方法,但即使没有文本显示在iPhone 6

我使用自定义字体,字体文件添加到正确投影。

更新:刚刚更新到Xcode的8.0,iOS上的10模拟器,MyLabel正在显示什么。

下面是执行:

头文件:

#import <UIKit/UIKit.h> 

IB_DESIGNABLE 

@interface MyLabel : UILabel 

@property (nonatomic) IBInspectable CGFloat letterSpacing; 
@property (nonatomic) IBInspectable CGFloat lineSpacing; 

@end 

实现文件:

#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]; 
    } 
} 
} 

要调试,我创建了一个新的单一视图的应用程序,并添加MyLabel.h和MyLabel.m文件到项目。

我使用的Xcode 8和iPhone 5S模拟器。

但结果都是一样正被MyLabel不会显示任何文本。

我感到沮丧,从MyLabel.hMyLabel.m除了MyLabel.h属性删除了所有的代码,整体类看起来象下面这样:

头文件:

#import <UIKit/UIKit.h> 

IB_DESIGNABLE 

@interface MyLabel : UILabel 

@property (nonatomic) IBInspectable CGFloat letterSpacing; 
@property (nonatomic) IBInspectable CGFloat lineSpacing; 

@end 

执行文件:

#import "MyLabel.h" 

@implementation MyLabel 

@end 

即使使用上面的代码,MyLabel也没有显示任何文本。

现在我决定重新命名像下面的属性:

@property (nonatomic) IBInspectable CGFloat ltrSpacing; 
@property (nonatomic) IBInspectable CGFloat lnSpacing; 

令人惊讶的MyLabel开始显示文本,怪异。

然后我重新介绍了我的逻辑,一切工作正常。

我认为我选择的房产名称与UILabel的部分私人房产相冲突,但我不确定具体原因。

+0

看到相同的行为。有一个名为“lineSpacing”的属性会阻止文本在我的子类中绘制。 – user2393462435