私は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];
}
}
}
同じ現象が見られます。 "lineSpacing"という名前のプロパティを持つと、テキストがサブクラスで描画されなくなりました。 – user2393462435