IOS6 字体高亮显示
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);
IOS6 字体高亮显示的更多相关文章
- Python学习-终端字体高亮显示1
Python学习-终端字体高亮显示 1.采用原生转义字符序列,对Windows有的版本不支持(比如win7),完美支持Linux 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显 ...
- Python学习-终端字体高亮显示
1.采用原生转义字符序列,对Windows有的版本不支持(比如win7),完美支持Linux 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以 ...
- ios开发之 -- NSString指定字体高亮显示
一个简单的小需求,就是在一个字符串里面,指定一部分字节高亮显示,代码如下: NSString *descStr = @"需要高亮显示的字符"; NSString *nickStr ...
- 精品自用IDEA插件吐血推荐
Alibaba Java Coding Guidelines 该插件集成了Alibaba代码编程规范,可以帮你找出你的代码怪味道 .gitignore 当你使用Git作为版本工具的时候,想要快速的添加 ...
- [C1W3] Neural Networks and Deep Learning - Shallow neural networks
第三周:浅层神经网络(Shallow neural networks) 神经网络概述(Neural Network Overview) 本周你将学习如何实现一个神经网络.在我们深入学习具体技术之前,我 ...
- java学习笔记(2)注释、public lcass、class、标识符、字面值、变量
java学习笔记(1)中说过了java的一些基础知识,发展史,特点,编译和运行过程,配置环境变量等,接下来开始介绍java的语法等只是点 关于java源程序中的注释: *什么是注释?注释的作用是什么 ...
- iOS 在UILabel显示不同的字体和颜色(ios6 and later)
在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的at ...
- 兼容PC手机端字体
各平台的主流字体支持情况 各系统的默认字体和常用字体: 系统 默认西文字体 默认中文字体 其他常用西文字体 其他常用中文字体 Windows 宋体 宋体 Tahoma.Arial.Verdana.Ge ...
- iOS6、7、8、9新特性汇总和适配说明
iOS6新特性 一.关于内存警告 ios6中废除了viewDidUnload,viewWillUnload这两个系统回调,收到内存警告时在didReceiveMemoryWarning中进行相关的处理 ...
随机推荐
- 【 D3.js 高级系列 — 9.0 】 交互式提示框
一般来说,图表中不宜存在过多文字.但是,有时需要一些文字来描述某些图形元素.那么,可以实现一种交互:当用户鼠标滑到某图形元素时,出现一个提示框,里面写有描述文字.这是一种简单.普遍的交互式,几乎适用于 ...
- 【C#学习笔记】读SQL Server2008
using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static voi ...
- LeetCode: 3SumClosest
Title : Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- liunx上运行mybase
mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累. 以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...
- 安装oracle XML Database 组件
近期部署项目数据库,编译包时遇到错误: PACKAGE CTG.CTG_CSB_COMMON 编译错误 错误:PLS-00201: identifier 'XMLDOM' must be dec ...
- MySQL 索引详解
本文介绍了数据库索引,及其优.缺点.针对MySQL索引的特点.应用进行了详细的描述.分析了如何避免MySQL无法使用,如何使用EXPLAIN分析查询语句,如何优化MySQL索引的应用. 索引是一种特殊 ...
- 【c++内存分布系列】单继承
父类包括成员函数.静态函数.静态方法,子类包括成员函数.静态函数.静态方法的情况与一个类时完全一致,这里就不做分析了.子类单独包含虚函数时继承无关,也不做分析了. 一.父类子类都为空 #include ...
- bjfu1252 贪心
题目意思是给出一些开区间,这些区间有的相交,有的不相交,问你能否选出一些区间,使这些区间之间都不相交,并且选出的区间数最大. 这是个典型的贪心问题了.按区间的结束位置排序,然后顺序地选取区间,只要当前 ...
- Shell获取Aix/linux/unix机器上db2和os的信息并上传到指定服务器
(之前写过一篇类似的文章,当时传输文件用的是ftp,因为项目觉得ftp不够安全所以这次换成了scp,同时对脚本的一些地方也做了一些调整) 其实做这个东西还是因为项目的需求,需要获取某些机器(目前主要是 ...
- 【Windows核心编程】重载类成员函数new / new[] / delete / delete[]
// Heap.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...