我创建了 UILabel 子类,它是 IB_DESIGNABLE 并且几乎没有 IBInspectable 属性。
我可以在所有 iPhone 模拟器和界面生成器中看到属性文本。
我可以在运行 iOS 9.3 的 iPhone 5c 中看到文本。
但对于同时运行 iOS 9.3 的 iPhone 6 系列设备,没有显示任何文本。
为了调试,我将背景颜色设置为自定义标签,我可以看到标签框架填充了给定的颜色,但没有显示文本。
我什至尝试评论 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];
}
}
}
最佳答案
为了调试,我创建了一个新的单 View 应用程序并将 MyLabel.h 和 MyLabel.m 文件添加到项目中。
我使用了 Xcode 8 和 iPhone 5S 模拟器。
但结果是一样的,MyLabel 没有显示文本。
让我很沮丧,删除了 MyLabel.h 和 MyLabel.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
的一些私有(private)属性冲突,但我不确定确切原因。
关于ios - 自定义 UILabel 不显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704573/