I'm attempting to set the background color/highlight just the text within a UILabel
. The issue is that the line breaks and spaces added to the UILabel
to keep the text centered are also being highlighted.
Notice the spacing before the last line in the UILabel
is highlighted. Also, the beginning and end of any new lines are also highlighted.
I'm creating the example above with the following code:
-(void)createSomeLabel {
// Create and position my label
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width - 40,
self.view.frame.size.height - 300)];
someLabel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
someLabel.textAlignment = NSTextAlignmentCenter;
someLabel.textColor = [UIColor whiteColor];
someLabel.lineBreakMode = NSLineBreakByWordWrapping;
someLabel.numberOfLines = 0;
[self.view addSubview:someLabel];
// This string will be different lengths all the time
NSString *someLongString = @"Here is a really long amount of text that is going to wordwrap/line break and I don't want to highlight the spacing. I want to just highlight the words and a single space before/after the word";
// Create attributed string
NSMutableAttributedString *someLongStringAttr=[[NSMutableAttributedString alloc] initWithString:someLongString attributes:nil];
// Apply background color
[someLongStringAttr addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithWhite:0 alpha:0.25]
range:NSMakeRange(0, someLongStringAttr.length)];
// Set text of label
someLabel.attributedText = someLongStringAttr;
}
The output I'd like to achieve is to highlight only the text and the spaces between the words if there is only one space. The length of the text and the size of the UILabel
will constantly be different so hard coding a solution is not an option unfortunately.
Starting at iOS 10.3 the same code in question now produces the desired result. Not sure if this is a bug or a new feature.
I've faced same issue and found out easier solution without huge performance costs. You can simply add TTTAttributedLabel to your project.
My demo project for the question:
It seemed to me that the line break was the problem. My idea was to try and know when the UILabel would add a line break and then just remove that character from the range of characters being highlighted.
It appears that you can't just ask UILabel where the line breaks are going to be but you can check what the size of an NSString will be when you add it to a label. Using this information you can increment through each character constantly checking the height, and when the height changes you know you have a new line.
I have made an example that takes the Label's string and separates it into its individual lines that will appear in the UILabel. Once I have each line, I just set the background color on each line instead of the whole string. This eliminates and background colors being set on line breaks.
There are probably better solutions, and this one could probably be optimized for better performance, but it's a starting point and it appears to work.