1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // UILabel+TD.m
- // smartRhino
- //
- // Created by tederen on 2019/10/22.
- // Copyright © 2019 tederen. All rights reserved.
- //
- #import "UILabel+TD.h"
- @implementation UILabel (TD)
- - (void)highlightString:(NSString *)string color:(UIColor *)color {
- NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:self.text];
- NSString *temp = nil;
- for(int i = 0; i < [self.text length] - [string length]; i++){
- temp = [self.text substringWithRange:NSMakeRange(i,[string length])];
- if ([temp isEqualToString:string]) {
- NSRange range = NSMakeRange(i,[string length]);
- [att addAttribute:NSForegroundColorAttributeName value:color range:range];
- }
- }
- self.attributedText = att;
- }
- - (NSAttributedString *)subhighlightString:(NSString *)string color:(UIColor *)color {
- NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:self.text];
- NSString *temp = nil;
- for(int i = 0; i <= [self.text length] - [string length]; i++){
- temp = [self.text substringWithRange:NSMakeRange(i,[string length])];
- if ([temp isEqualToString:string]) {
- NSRange range = NSMakeRange(i,[string length]);
- [att addAttribute:NSForegroundColorAttributeName value:color range:range];
- }
- }
- return att;
- }
- - (void)highlightString:(NSString *)string color:(UIColor *)color withSpace:(CGFloat)space withFont:(UIFont*)font setLineSpace:(NSUInteger)headIndent setTextSpace:(CGFloat)textspace{
- if (string.length == 0) {
- return;
- }
- NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithAttributedString:[self subhighlightString:string color:color]];
-
- NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
- paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
- paraStyle.alignment = NSTextAlignmentNatural;
- paraStyle.lineSpacing = space; //设置行间距
- paraStyle.hyphenationFactor = 1.0;
- paraStyle.firstLineHeadIndent = font.pointSize * headIndent;
- paraStyle.paragraphSpacingBefore = 0.0;
- paraStyle.headIndent = 0;
-
- NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@(textspace)
- };
-
- [att addAttributes:dic range:NSMakeRange(0,self.text.length)];
- self.attributedText = att;
- }
- @end
|