ios6之前在一个字符串中如果也让某个字体高亮或者特殊显示(如: 关注[ ]),需要用单独一个的标签进行显示,或者利用CoreText进行字体绘绘制,非常麻烦;

现在IOS6 中TextView,label,textField中新增了这样的一个属性NSAttributedString  只能应用IOS6

@property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil

利用NSAttributedString构建一个新的属性字符串,你就可以对他进行操作,删除,增加,字体格式化,设置某个字符前景色,字体颜色等...

如上图所示,直接上代码

- (void)setupTextView
{
_textView = [[UITextView alloc] initWithFrame:self.view.frame];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; NSString *textToAdd = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.\r\rThis text view can also use attributed strings."; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textToAdd]; // make red text
[attrString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange([attrString length] - 19, 19)]; // make blue text
[attrString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange([attrString length] - 23, 3)];
[attrString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:1]
range:NSMakeRange([attrString length] - 23, 3)]; [self.textView setAttributedText:attrString]; self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.scrollEnabled = YES; [self.view addSubview:self.textView];
}

NSForegroundColorAttributeName // 代表字体的前景色  下面所有进行字体格式化的标签

/* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.

*/

UIKIT_EXTERN NSString *const NSFontAttributeName NS_AVAILABLE_IOS(_0);                // UIFont, default Helvetica(Neue) 12

UIKIT_EXTERN NSString *const NSParagraphStyleAttributeName NS_AVAILABLE_IOS(_0);      // NSParagraphStyle, default defaultParagraphStyle

UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(_0);     // UIColor, default blackColor

UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(_0);     // UIColor, default nil: no background

UIKIT_EXTERN NSString *const NSLigatureAttributeName NS_AVAILABLE_IOS(_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures, 2: all ligatures (Note: 2 is unsupported on iOS)

UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(_0);                // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)

UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(_0);  // NSNumber containing integer, default 0: no strikethrough

UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(_0);      // NSNumber containing integer, default 0: no underline

UIKIT_EXTERN NSString *const NSStrokeColorAttributeName NS_AVAILABLE_IOS(_0);         // UIColor, default nil: same as foreground color

UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(_0);         // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(_0);              // NSShadow, default nil: no shadow

/* An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.

*/

UIKIT_EXTERN NSString *const NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(_0);

最新文章

  1. 【代码笔记】iOS-由身份证号码返回性别
  2. Spring中常用的连接池配置
  3. leetcode : valid binary search tree
  4. [git]Git常用命令
  5. js中ajax如何解决跨域请求
  6. jquery获取html元素的绝对位置和相对位置
  7. sql server日期时间转字符串(转)
  8. JVM的GC机制及JVM的调优方法
  9. oracle中decode的一些巧妙用法
  10. web前端开发 --好多视频大集合--文化的传播者-杜恩德
  11. JAVA如何实现深拷贝
  12. FbinstTools制作多系统启动U盘(Windows+Linux)
  13. jQuery-day01-介绍 和 选择器获取元素
  14. [HAOI2008]糖果传递 结论题
  15. Java基础9-死锁;String;编码
  16. SpringCloud-day05-服务调用Ribbon
  17. Nginx+IIS简单的部署
  18. CISCO ACL配置(目前)
  19. 2018.4.24 快排查找第K大
  20. [Artoolkit] Framework Analysis of nftSimple

热门文章

  1. erl0004 - ets 安全遍历
  2. RTP封装h264
  3. DirectDraw 直接显示RGB图象的最简单实现
  4. Android02--debug.keystore的注册信息
  5. SharePoint 2010中列表Add和Delete事件解析
  6. LXD 2.0 系列(二):安装与配置
  7. SQL遍历字符串的方法
  8. C语言char[]和char*比较
  9. 【跟我一起学Python吧】Python解释执行原理
  10. FS,FT,DFS,DTFT,DFT,FFT的联系和区别