2016-09-26 106 views
0

我已經創建了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]; 
    } 
} 
} 

回答

1

要調試,我創建了一個新的單一視圖的應用程序,並添加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