2016-09-26 14 views
0

私はIB_DESIGNABLEであり、IBInspectableプロパティがほとんどないUILabelサブクラスを作成しました。カスタムUILabelでテキストが表示されない

すべてのiPhoneシミュレータとインタフェースビルダーで属性付きのテキストが表示されます。

iOS 9.3を実行しているiPhone 5cのテキストを見ることができます。

iOS 9.3を実行しているiPhone 6シリーズのデバイスのテキストは表示されません。

カスタムラベルに背景色を設定すると、指定した色で塗りつぶされたラベルフレームが表示されますが、テキストは表示されません。

私もapplyStlyeOnTextコメントしてみてください:方法を、しかし、その後もテキストがiPhone 6.私はカスタムフォントを使用しています

に表示されていないが、フォントファイルが正常にプロジェクトに追加されます。

アップデート:iOS 10シミュレータのXcode 8.0に更新されました。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で表示されていたテキストと同じではありませんでした。

ヘッダーファイル:

#import <UIKit/UIKit.h> 

IB_DESIGNABLE 

@interface MyLabel : UILabel 

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

@end 

は、私をイライラMyLabel.hMyLabel.hのプロパティを除くMyLabel.mからすべてのコードを削除し、全体的なクラスは以下のように見えました

実装ファイル:

#import "MyLabel.h" 

@implementation MyLabel 

@end 

上記のコードでも、テキストはMyLabelで表示されませんでした。

は今、私は以下のようなプロパティの名前を変更することを決定した:

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

驚くべきことにMyLabelは奇妙なテキストを、表示を開始しました。

私は自分のロジックを再導入し、すべて正常に動作しています。

私が選んだプロパティ名はUILabelのプライベートプロパティのいくつかと矛盾していたと思いますが、正確な理由はわかりません。

+0

同じ現象が見られます。 "lineSpacing"という名前のプロパティを持つと、テキストがサブクラスで描画されなくなりました。 – user2393462435

関連する問題