IOS中使用NSAttributedString灵活创建标签

灵活使用NSAttributedString可以更轻松的创建出内容复杂的标签。需要注意一点:如果一个label设置了这个属性,那它其他的设置都将失效。

首先,我们初始化一个NSMutableAttributedString对象。

//通过字符串初始化
//- (instancetype)initWithString:(NSString *)str;
//通过字符串和属性字典直接初始化
//- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
//通过自身对象初始化
//- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

 NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc]initWithString:@"[email protected]#你好么QWE"];

可以通过下面两个函数对attrebute字符串进行设置与修改

//可以替换字符
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
//属性设置
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
//设置一定范围内字符属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

字典的键值对应如下:

//kCTFontAttributeName 这个键是字体的名称 必须传入CTFont对象
//kCTKernAttributeName 这个键设置字体间距 传入必须是数字对象 默认为0
//kCTLigatureAttributeName  这个键设置连字方式 必须传入CFNumber对象
//kCTParagraphStyleAttributeName  段落对其方式
//kCTForegroundColorAttributeName 字体颜色 必须传入CGColor对象
//kCTStrokeWidthAttributeName 笔画宽度 必须是CFNumber对象
//kCTStrokeColorAttributeName 笔画颜色
//kCTSuperscriptAttributeName 控制垂直文本定位 CFNumber对象
//kCTUnderlineColorAttributeName 下划线颜色
[attribute addAttribute:(NSString*)kCTKernAttributeName value:@5 range:NSMakeRange(0, 5)];
[attribute addAttribute:(NSString *)kCTFontAttributeName
                        value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,
                                                       14,
                                                       NULL))
                        range:NSMakeRange(0, 4)];
    [attribute addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                        value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]
                        range:NSMakeRange(0, 4)];

通过测试,发现上面有些键值并没有作用,可以替换下面的方法,效果相同,不同的地方在于其传值的类型不同,下面的方法更加方便(使用UIFont UIColor NSString 和一些系统枚举)

 NSParagraphStyleAttributeName
NSForegroundColorAttributeName
NSBackgroundColorAttributeName
NSLigatureAttributeName
NSKernAttributeName
NSStrikethroughStyleAttributeName
NSUnderlineStyleAttributeName
NSStrokeColorAttributeName
 NSStrokeWidthAttributeName
 NSShadowAttributeName
 NSTextEffectAttributeName
NSAttachmentAttributeName
 NSLinkAttributeName
 NSBaselineOffsetAttributeName
 NSUnderlineColorAttributeName
NSStrikethroughColorAttributeName
NSObliquenessAttributeName
 NSExpansionAttributeName
 NSWritingDirectionAttributeName
NSVerticalGlyphFormAttributeName
时间: 2024-08-06 07:54:49

IOS中使用NSAttributedString灵活创建标签的相关文章

iOS中转义后的html标签如何还原

最近用swift做一个公司的小项目,遇到一个问题,就是通过api获取的html文本中的标签都已经被转义了, <p class="MsoNormal" align="left" style="vertical-align:baseline;background:white;">\n\t<br />\n</p>\n<p class="MsoNormal" align="left&

ios中静态库的创建和使用、制作通用静态库(Cocoa Touch Static Library)

创建静态库可能出于以下几个理由: 1.你想将工具类代码或者第三方插件快捷的分享给其他人而无需拷贝大量文件.2.你想让一些通用代码处于自己的掌控之下,以便于修复和升级.3.你想将库共享给其他人,但不想让他们看到你的源代码. Xcode6创建静态库详解(Cocoa Touch Static Library) 一.创建静态库文件 打开Xcode, 选择File ----> New ---> Project. 新建工程. 选择iOS ----> Framework & Library -

iOS开发:iOS中的多控制器管理

iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Apply" bundle:nil]; SchemeViewController *schemeVC = [storyboard instantiateViewControllerWithIdentifier:@"SchemeViewController"]; 2.指定xib文

cocos2dx中创建标签CCLabel的三种方法及特点

创建标签的三种方式:1.CCLabelTTF     (True Type Font,又叫本地字体)这是最简单,也是最常用的方式,不依赖于资源文件,也不依赖于某个系统,所指定的字体如果系统没有,则会提供默认的字体,使用相当方便创建方式:CCLabelTTF *ttf=CCLabelTTF::create("GAME","Courier New",20);//第一个参数表示要显示的标签内容,第二个参数表示要使用的字体,第三个参数表示字体的大小如果要使用的字体,系统没有

IOS中标签视图和Block

标签视图控制器 - UITabBarController 自定义UITabBarItem 自定义导航栏和标签栏背景图片 三个视图控制器的综合使用 一. 标签视图控制器  UITabBarController UITabBarController有以下重要属性 viewControllers   显示的视图控制器 tabBar                  标签栏 delegate                协议 tabBar 是 UITabBar对象, 包含多个UITabBarItem,

iOS中创建数组的简易方法与手势的使用

其实做外包也是挺不错,虽然累点,但是一天天的知识的扩展不少,下面是今天的收获 ①初始化数组一字典的方法 @[] 初始化不可变数组 @{} 初始化不可变字典 ②手势的使用 iOS中处理手势之前是用四个方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touch

iOS中plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取功能创建一个test.plist文件,textInput作为输入,displayLabel作为显示,有一个按钮来触发保持程序triggerStorage: -(void)triggerStorage { displayLabel.text = textInput.text; NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)

ios中创建可以拖动的view原理和实现详解

有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIE

iOS中NSAttributedString的使用--对关键字着色,以及处理html实例

1,最近项目中用到了一个功能,一个很好的功能.就是用户在搜索的时候,搜索结果出来后对你输入的关键字进行红色标记.这样用户就很请楚的看到自己输入什么后会出现什么样子的结果.还有一个功能是,现在有一段文字了,但是要对其中的某些字符串进行着色处理,这个时候NSAttibutedString起到了非常大的作用.以下是我写好的一段代码,各位可以拿去用,非常方便的处理好. #import <Foundation/Foundation.h> @interface NSAttributedString (Co