【问题标题】:How to highlight only text in UILabel - IOS如何在 UILabel 中仅突出显示文本 - IOS
【发布时间】:2013-04-30 00:56:31
【问题描述】:

我只想突出显示UILabel 中的文本,我尝试通过给backgroundColor 提供标签,但它突出显示空白也看起来不太好。那么有没有办法在不调整UILabel大小的情况下突出显示文本。

请检查图片,这里的标签比文字大(居中对齐)

谢谢。

【问题讨论】:

  • 您必须使用NSAttributedString
  • @MikeWeller 你能提供更多细节吗...
  • 我认为您希望背景清晰。设置 label.backgroundColor = [UIColor clearColor];Label.highlightedTextColor = [UIColor greenColor];
  • 您是在 .xib 中添加标签还是以编程方式添加标签?
  • @pm444 仅限 xib,但我可以通过编程方式控制。

标签: iphone ios cocoa-touch uilabel xib


【解决方案1】:

试试这个

MTLabel

    MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60) 
                                  andText:@"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
        [label4 setLineHeight:22];
        [label4 setFont:[UIFont fontWithName:@"Helvetica" size:30]];
        [label4 setAdjustSizeToFit:YES];
        [label4 setMinimumFontSize:15];
        [label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
        [self.view addSubview:label4];

【讨论】:

    【解决方案2】:

    您可以拥有一个UIImageView,设置您选择的背景颜色,然后将您的UIlabel 放在上面。 那肯定会解决你的问题。 这是解决方法代码:

     UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
     [imgView setBackgroundColor:[UIColor brownColor]];
     [self.view addSubview:imgView];
    
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextAlignment:UITextAlignmentCenter];
    label.text = @"My Label";
    [imgView addSubview:label];
    
    
    [label release];
    [imgView release];
    

    您也可以使用 Interface Builder。

    【讨论】:

    • 请说明拒绝投票的原因!所以我可以更正我的答案。
    【解决方案3】:

    这将在文本后面添加一个子视图,大小正确:

    CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
    NSLog(@"%.1f | %.1f", size.width, size.height);
    NSLog(@"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
    
    UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [highlightView setBackgroundColor:[UIColor greenColor]];
    [self.view insertSubview:highlightView belowSubview:label];
    [highlightView setCenter:label.center];
    

    别忘了:[label setBackgroundColor:[UIColor clearColor]];

    【讨论】:

    • 如果文本跨越多行,这将无法正常工作。这个绿色视图只能形成单个矩形。如果第二行只有 1 个单词,则会突出显示整行。
    • 没错,但是如果你必须支持任何iOS NSMutableAttributedString,那么这可能是最简单的解决方案。
    【解决方案4】:

    大多数其他解决方案不考虑跨越多行的文本,同时仍然只突出显示文本,而且它们都非常hacky,涉及额外的子视图。

    iOS 6 及更高版本的解决方案是使用属性字符串:

    NSMutableAttributedString *s =
         [[NSMutableAttributedString alloc] initWithString:yourString];
    
    [s addAttribute:NSBackgroundColorAttributeName
              value:[UIColor greenColor]
              range:NSMakeRange(0, s.length)];
    
    label.attributedText = s;
    

    【讨论】:

    • 我还不知道 NSMutableAttributedString,无论如何它工作正常。谢谢。
    • 我已经尝试了上述过程。它对我来说效果不佳。 :| i.imgur.com/XhuVL9q.png?1
    • 这不像问题所期望的那样工作:它还会为自动换行创建的空白区域添加背景颜色。检查:Highlight just the text in a UILabel
    • 如果您只想突出显示文本而不是空格,则不能很好地处理多行文本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多