俄罗斯贵宾会 > 编程 > UILabel设置不同字体颜色俄罗斯贵宾会
UILabel设置不同字体颜色俄罗斯贵宾会

俄罗斯贵宾会,缺点: 必须先设置text或者attributedText上代码

  1. 设置给定range的文字颜色
  2. 设置给定range的文字字体

在开发中,经常要给label设置不同的字体、颜色。本人写了一个category便于以后开发,分享给大家。实现功能:

#import <UIKit/UIKit.h>@interface UILabel (AttributedString)-setAttributedTextWithAtts:(NSArray *)atts;-set_TextColor:(UIColor *)color range:range;-set_TextFont:font range:range;-set_DesignatedText:(NSString *)text color:(UIColor *)color;-set_DesignatedText:(NSString *)text font:font;-set_DesignatedTexts:(NSArray *)array color:(UIColor *)color;-set_DesignatedTexts:(NSArray *)array font:font;-set_DesignatedText:(NSString *)text attribute:(NSString *)name value:value;-set_DesignatedTexts:(NSArray *)texts attribute:(NSString *)name value:value;@end

#import "UILabel+AttributedString.h"#import "NSMutableAttributedString+Addtion.h"@implementation UILabel (AttributedString)-setAttributedTextWithAtts:(NSArray *)atts{ NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] init]; for (NSAttributedString *att in atts) { [attStr appendAttributedString:att]; } self.attributedText = attStr;}-set_TextColor:(UIColor *)color range:range{ if (self.attributedText.isNoEmpty && color) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setTextColor:color range:range]; self.attributedText = attStr; }}-set_TextFont:font range:range{ if (self.attributedText.isNoEmpty && font) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setTextFont:font range:range]; self.attributedText = attStr; }}-set_DesignatedText:(NSString *)text color:(UIColor *)color{ if (self.attributedText.isNoEmpty && text.isNoEmpty && color) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setDesignatedText:text color:color]; self.attributedText = attStr; }}-set_DesignatedText:(NSString *)text font:font{ if (self.attributedText.isNoEmpty && text.isNoEmpty && font) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setDesignatedText:text font:font]; self.attributedText = attStr; }}-set_DesignatedTexts:(NSArray *)array color:(UIColor *)color{ if (self.attributedText.isNoEmpty && array.isNoEmpty && color) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setDesignatedTexts:array color:color]; self.attributedText = attStr; }}-set_DesignatedTexts:(NSArray *)array font:font{ if (self.attributedText.isNoEmpty && array.isNoEmpty && font) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setDesignatedTexts:array font:font]; self.attributedText = attStr; }}-set_DesignatedText:(NSString *)text attribute:(NSString *)name value:value{ if (self.attributedText.isNoEmpty && text.isNoEmpty && name.isNoEmpty && value) { NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attStr setDesignatedText:text attribute:name value:value]; self.attributedText = attStr; }}-set_DesignatedTexts:(NSArray *)texts attribute:(NSString *)name value:value{ if (texts.isNoEmpty && name.isNoEmpty && value) { for (NSString *text in texts) { [self set_DesignatedText:text attribute:name value:value]; } }}@end

#import <Foundation/Foundation.h>#define range [NSValue valueWithRange:NSMakeRange]#define fontAtt NSFontAttributeName#define paragraphAtt NSParagraphStyleAttributeName#define fontColorAtt NSForegroundColorAttributeName@interface NSMutableAttributedString -(NSMutableAttributedString *)addAttributesWithArray:(NSArray *)attrs;//数组 name value range-(NSMutableAttributedString *)addAttributesWithDict:(NSDictionary *)attrs range:range;-(NSMutableAttributedString *)addAttributesWithDict:(NSDictionary *)dict;-setTextColor:(UIColor *)color range:range;-setTextFont:font range:range;-setDesignatedText:(NSString *)text color:(UIColor *)color;-setDesignatedText:(NSString *)text font:font;-setDesignatedTexts:(NSArray *)array color:(UIColor *)color;-setDesignatedTexts:(NSArray *)array font:font;-setDesignatedText:(NSString *)text attribute:(NSString *)name value:value;-setDesignatedTexts:(NSArray *)texts attribute:(NSString *)name value:value;@end

#import "NSMutableAttributedString+Addtion.h"@implementation NSMutableAttributedString -(NSMutableAttributedString *)addAttributesWithArray:(NSArray *)attrs{ for (NSArray *array in attrs) { if (array.count == 2) { [self addAttribute:array[0] value:array[1] range:NSMakeRange(0, self.length)]; }else{ [self addAttribute:array[0] value:array[1] range:[array[2] rangeValue]]; } } return self;}-(NSMutableAttributedString *)addAttributesWithDict:(NSDictionary *)attrs range:range{ for (NSString *key in attrs.allKeys) { [self addAttribute:key value:attrs[key] range:range]; } return self;}-(NSMutableAttributedString *)addAttributesWithDict:(NSDictionary *)dict{ for (id key in dict.allKeys) { if ([key isKindOfClass:[NSValue class]]) { [self addAttributesWithDict:dict[key] range:[key rangeValue]]; }else{ [self addAttributesWithDict:key range:[dict[key] rangeValue]]; } } return self;}-setTextColor:(UIColor *)color range:range{ if (self.length > 0 && color && [color isKindOfClass:[UIColor class]] && self.length >= NSMaxRange && NSMaxRange > 0) { [self addAttribute:NSForegroundColorAttributeName value:color range:range]; }}-setTextFont:font range:range{ if (self.length > 0 && font && [font isKindOfClass:[UIFont class]] && self.length >= NSMaxRange && NSMaxRange > 0) { [self addAttribute:NSFontAttributeName value:font range:range]; }}-setDesignatedText:(NSString *)text font:font{ if (self.length > 0 && text.length && font) { // NSRange range = [self.string rangeOfString:text]; NSArray *rangeArray = [self rangesOfString:text inString:self.string]; if (rangeArray.isNoEmpty) { for (NSValue *value in rangeArray) { [self addAttribute:NSFontAttributeName value:font range:[value rangeValue]]; } } // if (range.length > 0) { // [self addAttribute:NSFontAttributeName value:font range:range]; // } }}-setDesignatedText:(NSString *)text color:(UIColor *)color{ if (self.length > 0 && text.length && color) { // NSRange range = [self.string rangeOfString:text]; NSArray *rangeArray = [self rangesOfString:text inString:self.string]; if (rangeArray.isNoEmpty) { for (NSValue *value in rangeArray) { [self addAttribute:NSForegroundColorAttributeName value:color range:[value rangeValue]]; } } // if (range.length > 0) { // [self addAttribute:NSForegroundColorAttributeName value:color range:range]; // } }}-setDesignatedTexts:(NSArray *)array color:(UIColor *)color{ if (self.length && array.isNoEmpty && color) { for (NSString *text in array) { [self setDesignatedText:text color:color]; } }}-setDesignatedTexts:(NSArray *)array font:font{ if (self.length && array.isNoEmpty && font) { for (NSString *text in array) { [self setDesignatedText:text font:font]; } }}- (NSArray *)rangesOfString:(NSString *)searchString inString:(NSString *)str { if (searchString.isNoEmpty && str.isNoEmpty) { NSMutableArray *results = [NSMutableArray array]; NSRange searchRange = NSMakeRange(0, [str length]); NSRange range; while ((range = [str rangeOfString:searchString options:0 range:searchRange]).location != NSNotFound) { [results addObject:[NSValue valueWithRange:range]]; searchRange = NSMakeRange(NSMaxRange, [str length] - NSMaxRange; } return results; } return nil;}-setDesignatedText:(NSString *)text attribute:(NSString *)name value:value{ if (text.isNoEmpty && name.isNoEmpty && value && self.length) { NSArray *rangeArray = [self rangesOfString:text inString:self.string]; if (rangeArray.isNoEmpty) { for (NSValue *value1 in rangeArray) { [self addAttribute:name value:value range:[value1 rangeValue]]; } } }}-setDesignatedTexts:(NSArray *)texts attribute:(NSString *)name value:value{ if (self.length && texts.isNoEmpty && name.isNoEmpty && value) { for (NSString *text in texts) { [self setDesignatedText:text attribute:name value:value]; } }}@end

上一篇:UIWebView键盘处理 下一篇:截取视频当前时间的一帧图片
返回列表