标签:
使用方法:
FNLabel *textL = [[FNLabel alloc] initWithFrame:(CGRect){20,100,300,MAXFLOAT}];
NSString *textStr = @",攻击哦评价,发我家偶奇偶给,我看了您访问利率;赶紧弄魔法卡片,课堂为规范嘛呢;拉倒给我扑克牌,给我恐怕,比,客人可观评价,发我;赶紧弄魔法 卡片,课堂为规范嘛呢;拉倒给我扑克牌,给我恐怕,比,客人可观评价,发我家偶奇偶给,我看了您访";
textL.backgroundColor = [UIColor redColor];
[self.view addSubview:textL];
使用效果
核心方法:
文本属性NSMutableParagraphStyle与CoreText结合
使用注意:
1.不用设置numberOfLines,自动换行
2.默认行间距为5,可在104行更改,可以自己添加更多属性
3.string属性用于添加富文本,text为普通文本
代码----声明文件
#import <UIKit/UIKit.h> @interface FNLabel : UIView @property (nonatomic, strong)NSMutableAttributedString* string; @property (nonatomic, strong)UIFont* font; @property (nonatomic, strong)UIColor* textColor; - (void)setText:(NSString*)text; @end
代码----实现文件
1 #import "FNLabel.h" 2 #import <CoreText/CoreText.h> 3 4 @implementation FNLabel 5 6 - (id)initWithFrame:(CGRect)frame 7 { 8 self = [super initWithFrame:frame]; 9 if (self) { 10 self.backgroundColor = [UIColor clearColor]; 11 } 12 return self; 13 } 14 15 - (void)drawRect:(CGRect)rect 16 { 17 [self formatString]; 18 19 CGContextRef ctx = UIGraphicsGetCurrentContext(); 20 21 // 从文本空间到用户控件的转换矩阵 22 CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); 23 // 缩放范围 24 CGContextTranslateCTM(ctx,0, self.bounds.size.height); 25 // 调整正确的坐标系 26 CGContextScaleCTM(ctx, 1.0, -1.0); 27 28 // 1.由富文本得到framesetter 29 CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_string); 30 // 2.由self.bounds得到path 31 CGMutablePathRef path = CGPathCreateMutable(); 32 CGPathAddRect(path, NULL, self.bounds); 33 // 3.由framesetter(内涵文本内容),range(要画的范围),path文本矩形 得到CTFrameRef 34 CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [_string length]), path, NULL); 35 // 4.画文本 36 CTFrameDraw(frame, ctx); 37 38 CFRelease(path); 39 CFRelease(frame); 40 CFRelease(frameSetter); 41 } 42 #pragma mark - 普通的两端对齐文本 43 - (void)setText:(NSString *)text 44 { 45 _string = [[NSMutableAttributedString alloc] initWithString:text]; 46 if (_textColor) { 47 [_string addAttribute:NSForegroundColorAttributeName value:_textColor range:NSMakeRange(0, [_string length])]; 48 } 49 if (_font) { 50 [_string addAttribute:NSFontAttributeName value:_font range:NSMakeRange(0, [_string length])]; 51 } 52 [self setNeedsDisplay]; 53 } 54 #pragma mark - 保存整体文字颜色 55 - (void)setTextColor:(UIColor *)textColor 56 { 57 _textColor = textColor; 58 } 59 #pragma mark - 保存整体字体 60 - (void)setFont:(UIFont *)font 61 { 62 _font = font; 63 } 64 #pragma mark - 添加所有富文本属性 65 - (void)setString:(NSMutableAttributedString *)string 66 { 67 68 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string.string]; 69 70 _string = str; 71 // 设置文字颜色 72 if (_textColor == nil) { 73 _textColor = [UIColor blackColor]; 74 } 75 [_string addAttribute:NSForegroundColorAttributeName value:_textColor range:NSMakeRange(0, [string length])]; 76 if (_font == nil) { 77 _font = [UIFont systemFontOfSize:20]; 78 } 79 // 设置文字字体 80 [_string addAttribute:NSFontAttributeName value:_font range:NSMakeRange(0, [string length])]; 81 82 // 遍历添加所有字体文本属性 83 [string enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, _string.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { 84 if (value) { 85 [_string addAttribute:NSFontAttributeName value:value range:range]; 86 87 [self setNeedsDisplay]; 88 } 89 }]; 90 // 遍历添加所有颜色文本属性 91 [string enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, _string.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { 92 if (value) { 93 [_string addAttribute:NSForegroundColorAttributeName value:value range:range]; 94 95 [self setNeedsDisplay]; 96 } 97 }]; 98 } 99 #pragma mark - 添加两端对齐属性 100 - (void)formatString 101 { 102 NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 103 style.alignment = NSTextAlignmentJustified; 104 style.lineSpacing = 5; 105 [_string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, _string.length)]; 106 107 } 108 109 @end
标签:
原文地址:http://www.cnblogs.com/llinsline/p/5369088.html