首页 文章

更改UILabel文本但保留其余属性

提问于
浏览
13

我在使用故事板创建的ViewController中有一个UILabel . 标签文本的字体,大小和颜色,它的对齐方式 - 都在故事板中设置 . 故事板中的标签连接到我的文件中名为p_patientNameLbl的插座 .

现在我试图以编程方式更改标签的文本,如下所示:

[self.p_patientNameLbl setText:@"Vasya"];

我没看到新文本,直到我意识到故事板中的原始标签在黑色背景上是白色的,但显然在我更改了上面的标签文本之后,所有字体属性都已重置,现在它是一个黑色文本黑色背景,因此没见过 . 一旦我以编程方式设置颜色:

[self.p_patientNameLbl setTextColor:[UIColor whiteColor]];

我可以看到新标签,但所有其余的字体属性和对齐仍然是错误的 .

有没有办法只更改标签的文本,而无需以编程方式设置所有其余的属性?我想有必须有办法,因为我不想在代码中格式化我的界面!

6 回答

  • 18

    不是存储旧属性并使用更改的文本和存储属性创建新的NSAttributedString,而是一种相对简单的方法:

    NSMutableAttributedString *attStr = [label.attributedText mutableCopy];
    NSMutableString *str = attStr.mutableString;
    /* change str with new text */
    label.attributedText = attStr;
    
  • -1

    我已经为多个应用做了很多次 . 我总是在Storyboard中制作标签和文本,在编辑器中更改字体大小,颜色显示,alpha值等 . 但是之后如果我需要更改我多次做过的文本,我所做的就是这个 .

    p_patientNameLbl.textColor = [UIColor redColor];
    p_patientNameLbl.text = @"My Sample Text";
    

    而已 . 保留所有其他默认属性 . 除非我在你的问题中遗漏了什么?

  • -1

    之所以发生这种情况,是因为您告诉“界面”构建器采用在其文本上设置了预定义属性的标签,并使用纯文本替换此属性文本 . 而不是 setTextsetTextColor ,您必须使用 setAttributedText 并指定要传递给属性字符串的属性 .

    以下是如何以编程方式将属性字符串应用于标签的示例 . 我不知道有什么更好的方法,但使用它,您可以更改文本或文本颜色,只要您设置其他属性,所有更改都将正确应用 .

    [myLabel setAttributedText:[self myLabelAttributes:@"Some awesome text!"]];
    
    ....................
    
    - (NSMutableAttributedString *)myLabelAttributes:(NSString *)input
    {
        NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:input];
    
        [labelAttributes addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-5.0] range:NSMakeRange(0, labelAttributes.length)];
        [labelAttributes addAttribute:NSStrokeColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, labelAttributes.length)];
        [labelAttributes addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, labelAttributes.length)];
    
        return labelAttributes;
    }
    
  • 8

    这对我有用:

    - (void)setText:(NSString *)text withExistingAttributesInLabel:(UILabel *)label {
    
        // Check label has existing text
        if ([label.attributedText length]) {
    
            // Extract attributes
            NSDictionary *attributes = [(NSAttributedString *)label.attributedText attributesAtIndex:0 effectiveRange:NULL];
    
            // Set new text with extracted attributes
            label.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    
        }
    
    }
    
    ...
    
    [self setText:@"Some text" withExistingAttributesInLabel:self.aLabel];
    
  • 1

    Swift中的一行实现:

    label.attributedText = NSAttributedString(string: "text", attributes: label.attributedText!.attributesAtIndex(0, effectiveRange: nil))
    
  • 8

    0x7fffffff,谢谢你的提示,这对我有用 . 在iOS6中,这里的一些其他建议不起作用,例如 . 提取属性并重新应用它们 .

    在我的情况下,我失去了很多有用的属性,包括字体,所以这是我的解决方案:

    - (NSMutableAttributedString *)SetLabelAttributes:(NSString *)input col:(UIColor *)col size:(Size)size {
    
    NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:input];
    
    UIFont *font=[UIFont fontWithName:@"Helvetica Neue" size:size];
    
    NSMutableParagraphStyle* style = [NSMutableParagraphStyle new];
    style.alignment = NSTextAlignmentCenter;
    
    [labelAttributes addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSForegroundColorAttributeName value:col range:NSMakeRange(0, labelAttributes.length)];
    
    return labelAttributes;
    }
    

    然后将它应用于UILabel,如下所示:

    [self.myLabel setAttributedText:[self CurrentSetLabelAttributes:[NSString stringWithFormat:@"%d", myClass.text] col:[UIColor redColor]  size:50.0f]];
    

相关问题