11

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.

Please check the image, here labels are bigger than the text (center aligned)

enter image description here

Thanx.

Venk
  • 5,949
  • 9
  • 41
  • 52
Newbee
  • 3,231
  • 7
  • 42
  • 74

4 Answers4

22

Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.

An iOS 6 and later solution is to use attributed strings:

NSMutableAttributedString *s =
     [[NSMutableAttributedString alloc] initWithString:yourString];

[s addAttribute:NSBackgroundColorAttributeName
          value:[UIColor greenColor]
          range:NSMakeRange(0, s.length)];

label.attributedText = s;
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • I have no idea about NSMutableAttributedString yet, anyhow its working fine. thanks. – Newbee May 06 '13 at 11:12
  • 3
    I have tried the above process. It didn't work well with me. :| http://i.imgur.com/XhuVL9q.png?1 – Awais Tariq Mar 24 '14 at 10:53
  • This does not work as expected by the question: it also add background color to the empty spaces created by word wrapping. Check: [Highlight just the text in a UILabel](http://stackoverflow.com/questions/31293873/highlight-just-the-text-in-a-uilabel?rq=1) – Damasio Dec 10 '15 at 11:07
  • won't work well with multiline texts if you want to highlight just the text and not the empty spaces – SPS Jan 21 '21 at 09:00
3

This will add a subview behind the text, with the correct size:

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];

And don't forget: [label setBackgroundColor:[UIColor clearColor]];

xapslock
  • 1,119
  • 8
  • 21
  • 3
    This won't work correctly if text is wrapping across multiple lines. This green view can only form single rectangles. If the second line only has 1 word, the entire line will be highlighted. – Mike Weller May 06 '13 at 10:35
  • That's right, but if you have to support any iOS <6 and can't use `NSMutableAttributedString`, then this may be the simplest solution. – xapslock May 06 '13 at 10:45
1

try this

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];
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
-3

You can have a UIImageView, set its background color of your choice then place your UIlabel on it. That will surely solve your problem. Here is Workaround code :

 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];

You can also achieve the same using Interface Builder.

gagan sharma
  • 256
  • 1
  • 4
  • 18