This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)boldRange:(NSRange)range; | |
- (void)boldSubstring:(NSString*)substring; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation UILabel (BoldRangeLabel) | |
- (void)boldRange:(NSRange)range { | |
if (![self respondsToSelector:@selector(setAttributedText:)]) { | |
return; | |
} | |
NSMutableAttributedString *attributedText; | |
if (!self.attributedText) { | |
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text]; | |
} else { | |
attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; | |
} | |
[attributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], | |
NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] | |
} | |
range:range]; | |
self.attributedText = attributedText; | |
} | |
- (void)boldSubstring:(NSString*)substring { | |
NSRange range = [self.text rangeOfString:substring]; | |
if (![self respondsToSelector:@selector(setAttributedText:)]) { | |
return; | |
} | |
NSMutableAttributedString *attributedText; | |
if (!self.attributedText) { | |
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text]; | |
} else { | |
attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; | |
} | |
while (range.location != NSNotFound) | |
{ | |
[attributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], | |
NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] | |
} | |
range:range]; | |
NSRange rangeToSearch; | |
rangeToSearch.location = range.location + range.length; | |
rangeToSearch.length = self.text.length - rangeToSearch.location; | |
range = [self.text rangeOfString:substring options:0 range:rangeToSearch]; | |
[attributedText setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor], | |
NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize] | |
} | |
range:range]; | |
self.attributedText = attributedText; | |
} | |
} | |
@end |