我已經創建了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];
}
}
}
看到相同的行爲。有一個名爲「lineSpacing」的屬性會阻止文本在我的子類中繪製。 – user2393462435