Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILabel属性化文本呈现不正确_Ios_Objective C_Uilabel_Nsattributedstring - Fatal编程技术网

Ios UILabel属性化文本呈现不正确

Ios UILabel属性化文本呈现不正确,ios,objective-c,uilabel,nsattributedstring,Ios,Objective C,Uilabel,Nsattributedstring,我一直在使用属性文本设置UILabel以获得所需的字体大纲,并且我经常更改UILabel的attributedText属性。它似乎有大约50%的时间是在不删除旧文本的情况下呈现新文本而不是旧文本。现在,我的代码如下所示: // Attributes initialization self.labelAttributes = [NSMutableDictionary dictionary]; [self.labelAttributes setObject: [UIFont fontWithName

我一直在使用属性文本设置UILabel以获得所需的字体大纲,并且我经常更改UILabel的attributedText属性。它似乎有大约50%的时间是在不删除旧文本的情况下呈现新文本而不是旧文本。现在,我的代码如下所示:

// Attributes initialization
self.labelAttributes = [NSMutableDictionary dictionary];
[self.labelAttributes setObject: [UIFont fontWithName:@"Helvetica Neue" size:21] forKey: NSFontAttributeName];
[self.labelAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[self.labelAttributes setObject: [NSNumber numberWithFloat: -3.0] forKey: NSStrokeWidthAttributeName];
[self.labelAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];

// Clear UILabel attributedString
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:self.labelAttributes];
// Also attempted this with nil;


// Set UILabel to string, where self.userName and self.userAge are just regular strings. 
NSString *labelString = [NSString stringWithFormat:@"%@, %@", self.userName, self.userAge];
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:labelString attributes:self.labelAttributes];
当它工作时,看起来是这样的:

// Attributes initialization
self.labelAttributes = [NSMutableDictionary dictionary];
[self.labelAttributes setObject: [UIFont fontWithName:@"Helvetica Neue" size:21] forKey: NSFontAttributeName];
[self.labelAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[self.labelAttributes setObject: [NSNumber numberWithFloat: -3.0] forKey: NSStrokeWidthAttributeName];
[self.labelAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];

// Clear UILabel attributedString
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:self.labelAttributes];
// Also attempted this with nil;


// Set UILabel to string, where self.userName and self.userAge are just regular strings. 
NSString *labelString = [NSString stringWithFormat:@"%@, %@", self.userName, self.userAge];
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:labelString attributes:self.labelAttributes];

当它不起作用时,看起来是这样的:

// Attributes initialization
self.labelAttributes = [NSMutableDictionary dictionary];
[self.labelAttributes setObject: [UIFont fontWithName:@"Helvetica Neue" size:21] forKey: NSFontAttributeName];
[self.labelAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[self.labelAttributes setObject: [NSNumber numberWithFloat: -3.0] forKey: NSStrokeWidthAttributeName];
[self.labelAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];

// Clear UILabel attributedString
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:self.labelAttributes];
// Also attempted this with nil;


// Set UILabel to string, where self.userName and self.userAge are just regular strings. 
NSString *labelString = [NSString stringWithFormat:@"%@, %@", self.userName, self.userAge];
self.userLabel.attributedText = [[NSAttributedString alloc] initWithString:labelString attributes:self.labelAttributes];

这似乎是最后一个用户名,加上彼此重叠的当前用户名


我想不出一个好办法来保证标签被清除,也不知道如何调试它。(我曾尝试在XCode 6中使用可视化调试,但它仍然认为这只是一个标签,新用户的文本作为文本属性。)

很有趣。我真的不知道为什么这样不行。你的代码看起来不错。我能想到的唯一一件事是,如果这是表格视图单元格中的一个标签,它在屏幕外然后在屏幕上被重置?否则就不知道了

这是另一种对我来说同样有效的方法

NSString *labelString = [NSString stringWithFormat:@"%@, %@", self.userName, self.userAge];
self.userLabel.text = labelString;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithAttributedString:self.userLabel.attributedText];
[attStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica Neue" size:21] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0f] range:NSMakeRange(0, self.userLabel.length)];
[attStr addAttribute:NSStrokeColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.userLabel.length)];
//or 1 liner
//[attStr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica Neue" size:21], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, [NSNumber numberWithFloat:-3.0f], NSStrokeWidthAttributeName, [UIColor blackColor], NSStrokeColorAttributeName, nil] range:NSMakeRange(0, self.userLabel.length)];
[self.userLabel setAttributedText:attStr];
此外,我不明白为什么不能将标签设置为从这些属性开始(如果它们在整个标签中没有改变)。 你可以这样做:

NSString *labelString = [NSString stringWithFormat:@"%@, %@", self.userName, self.userAge];
self.userLabel.text = labelString;
self.userLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:21];
self.userLabel.textColor = [UIColor blackColor];
//Then set the stroke with one attribute

希望这有帮助!愉快的编码。

我想我以前在UITableCell重用和动态表单元格高度(在运行时确定)方面见过这种情况。如果我没记错的话,当单元格被设置为高度0时会发生这种情况


如果您在上述场景中,请尝试在UITableViewCell或最上面的重用视图中将
clipsToBounds
打开。当包含视图的帧大小或高度设置为零时,启用
clipstobunds
会导致标签等不会流出视图。

默认情况下,标签应该已经正确,但您仍可以尝试:

self.userLabel.clearsContextBeforeDrawing = YES;
self.userLabel.contentMode = UIViewContentModeRedraw;

您可以按照以下步骤进行操作:

self.userLabel.attributedText = [[NSAttributedString alloc] 
initWithString:@"string to both stroke and fill" 
attributes:@{
             NSStrokeWidthAttributeName: [NSNumber numberWithFloat:-3.0],
             NSStrokeColorAttributeName: [UIColor blackColor],
             NSForegroundColorAttributeName: [UIColor whiteColor]
             }
];

我怀疑这个问题与属性文本无关。我怀疑问题在于您正在重用的UITableViewCell中添加新的UILabel,而不是重用旧的UILabel。

您确定没有添加添加标签吗?是否在表格单元格中?在设置新值之前,无需清除标签。另外,您为标签属性构造字典的方式有点奇怪,也很难阅读。@rmaddy我考虑过这一点,但如果我正确阅读视图调试器,它只是一个视图:@sha我只是在设置新值之前开始清除标签,因为我希望它能在下一个值可用之前“重置”该值。在我开始这么做之前,我仍然在经历同样的错误。此外,您将如何构建字典以提高可读性?您确定标签不在可重用视图(如UITableViewCell)中吗?