NSMutableAttributedString富文本

一、ios5.0以前

1、首先导入CoreText.framework,并在需要使用的文件中导入:

#import<CoreText/CoreText.h>

2、创建一个NSMutableAttributedString:

  1. NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]
  2. autorelease];

非常常规的创建方式,接下来我们给它配置属性:

  1. //把this的字体颜色变为红色
  2. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
  3. value:(id)[UIColor redColor].CGColor
  4. range:NSMakeRange(0, 4)];
  5. //把is变为黄色
  6. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
  7. value:(id)[UIColor yellowColor].CGColor
  8. range:NSMakeRange(5, 2)];
  9. //改变this的字体,value必须是一个CTFontRef
  10. [attriString addAttribute:(NSString *)kCTFontAttributeName
  11. value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,
  12. 14,
  13. NULL)
  14. range:NSMakeRange(0, 4)];
  15. //给this加上下划线,value可以在指定的枚举中选择
  16. [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
  17. value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]
  18. range:NSMakeRange(0, 4)];
  19. return attriString;

3、NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:

在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同,Quartz2D的坐标系统在左下角

  1. -(void)drawRect:(CGRect)rect{
  2. [super drawRect:rect];
  3. NSAttributedString *attriString = getAttributedString();
  4. CGContextRef ctx = UIGraphicsGetCurrentContext();
  5. CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
  6. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
  7. CGMutablePathRef path = CGPathCreateMutable();
  8. CGPathAddRect(path, NULL, rect);
  9. CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
  10. CFRelease(path);
  11. CFRelease(framesetter);
  12. CTFrameDraw(frame, ctx);
  13. CFRelease(frame);
  14. }

4、另外方法使用

1)

  1. CATextLayer *textLayer = [CATextLayer layer];
  2. textLayer.string = getAttributedString();
  3. textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200);
  4. [self.view.layer addSublayer:textLayer];

CATextLayer可以直接支持NSAttributedString!

2) UILabel *label = [[UILabel alloc] init];

label.frame = CGRectMake(100, 100, 100, 40);

[label setAttributedText:attrTitle];

[self.view addSubview:label];

二、在iOS6之后,创建一个AttributedString变成了一件轻松的事情,<CoreText/CoreText.h>已经不需要导入了。如果我要设置字体的颜色,可以直接这样:

[textAttr addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(0, text.length)];

如果要计算一个NSAttributedString的size,使用NSAttributedString的这个API:

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);

但是需要注意一点,如果调用这个API的NSAttributedString不包含字体、行高等有利于计算的数据,那最终计算出来的size可能和实际有所出入。

时间: 2024-07-29 22:37:38

NSMutableAttributedString富文本的相关文章

NSMutableAttributedString(富文本)的简单使用

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, self.view.frame.size.width-40, self.view.fr

NSMutableAttributedString 富文本删除线的用法

#import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributedString *priceString = [[NSMutableAttributedString alloc] initWithString:priceStr]; [priceString addAttribute:NSFontAttributeName value:[UIFont systemFontOf

转载的一个富文本,挺实用的

文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 4.UITextKit简介 5.编程思想的相关思考 前言 富文本使用案例: 这里我自己也用了富文本实现了简单的却也是常用的例子: 对于最后面的¥50中划线这种设置,估计只有富文本最好用了. 在IOS或者Mac OS X通过UIKit提供的用来显示字符串控件有三个: UILable,UITextFi

iOS之富文本总结

文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 4.UITextKit简介 5.编程思想的相关思考 前言 富文本使用案例: 这里我自己也用了富文本实现了简单的却也是常用的例子: 对于最后面的¥50中划线这种设置,估计只有富文本最好用了. 在IOS或者Mac OS X通过UIKit提供的用来显示字符串控件有三个: UILable,UITextFi

富文本NSAttributedString与NSMutableAttributedString

NSAttributedString NSAttributedString用来处理字符串,使在同一字符串内显示出不同属性的字符.(例如:用来处理字符串)总之就是可以设置字符串中指定位置或指定范围内字符的属性. 创建一个NSAttributedString对象 - (instancetype)initWithString:(NSString *)aString   使用字符串初始化对象 - (instancetype)initWithAttributedString:(NSAttributedSt

富文本(NSMutableAttributedString)

干货: - (void)viewDidLoad { [super viewDidLoad]; NSString * tempStr = @"貌似这个叫富文本"; NSMutableAttributedString * attStr = [self stringTurnToAttributeStringWithString:tempStr Font:25 TextColor:[UIColor redColor] Range:NSMakeRange(5, 3)]; UILabel * la

ios中label富文本的设置

1.修改不同文字和颜色 // 创建一个富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈123456789"]; // 修改富文本中的不同文字的样式 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5

iOS常用技术-Label富文本

////  ViewController.m//  Label富文本////  Created by 大欢 on 16/1/19.//  Copyright © 2016年 bjsxt. All rights reserved.// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {    [super view

iOS富文本组件的实现—DTCoreText源码解析 数据篇

本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需要显示富文本的场景下代替低性能的UIWebView,来看看它是怎样解析和渲染HTML+CSS的,总体上分成两步: 数据解析—把HTML+CSS转换成NSAttributeString 渲染—用CoreText把NSAttributeString内容渲染出来,再加上图片等元素 本篇先介绍第一步,数据解