'UILabel'에 해당되는 글 1건

  1. 2018.05.16 [Objective-C] UILabel 해당 택스트 컬러 변경 (AttributedString)
Dev/Objective-C2018. 5. 16. 15:48
- (void)boldRange:(NSRange)range;
- (void)boldSubstring:(NSString*)substring;
@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
Posted by iDeveloper