今天在做项目的过程中,我们的设计师想要一种字体四周都带阴影的效果,但是我们平时使用的setShadowColor 和setShadowOffset是达不到这种效果,setShadowOffset 只能让阴影在上下,左右 方向中都只能显示一个方向,达不到文字的四周都有阴影的这种效果。随后我们在查阅iOS SDK之后,发现可以通过attributedText的属性来设置字体的样式,经过测试是可以的.

NSString *text = @"test";

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

NSShadow *shadow = [[NSShadow alloc] init];

[shadow setShadowColor:[UIColorcolorWithRed:0green:0blue:0alpha:0.5]];

[shadow setShadowBlurRadius:4.0];

[shadow setShadowOffset:CGSizeMake(0, 0)];

[attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, [attributedString length])];

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColorlightGrayColor] range:NSMakeRange(0, [attributedString length])];

[attributedString addAttribute:UIFontsystemFontOfSize value:18 range:NSMakeRange(0, [attributedString length])];

userName.attributedText = textLabelStr;

在上面这段代码中我们设置了字体大小,字体颜色,字体的阴影,字体阴影的大小。那么其实对字体样式进行设置的时候是可以限定样式应用在字符串中的位置。那么对于NSMutableAttributedString 我们可以添加哪些设置,SDK中描述如下:

NSFontAttributeName NS_AVAILABLE_IOS(6_0);                // UIFont, default Helvetica(Neue) 12

NSParagraphStyleAttributeName NS_AVAILABLE_IOS(6_0);      // NSParagraphStyle, default defaultParagraphStyle

NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default blackColor

NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default nil: no background

NSLigatureAttributeName NS_AVAILABLE_IOS(6_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

NSKernAttributeName NS_AVAILABLE_IOS(6_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)

NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0);  // NSNumber containing integer, default 0: no strikethrough

NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0);      // NSNumber containing integer, default 0: no underline

NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0);         // UIColor, default nil: same as foreground color

NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_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)

NSShadowAttributeName NS_AVAILABLE_IOS(6_0);              // NSShadow, default nil: no shadow

NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0);          // NSString, default nil: no text effect

NSAttachmentAttributeName NS_AVAILABLE_IOS(7_0);          // NSTextAttachment, default nil

NSLinkAttributeName NS_AVAILABLE_IOS(7_0);                // NSURL (preferred) or NSString

NSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0);      // NSNumber containing floating point value, in points; offset from baseline, default 0

NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0);      // UIColor, default nil: same as foreground color

NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0);  // UIColor, default nil: same as foreground color

NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0);         // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

NSExpansionAttributeName NS_AVAILABLE_IOS(7_0);           // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

NSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0);

NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0);

typedef NS_ENUM(NSInteger, NSUnderlineStyle) {

NSUnderlineStyleNone                                = 0x00,

NSUnderlineStyleSingle                              = 0x01,

NSUnderlineStyleThick NS_ENUM_AVAILABLE_IOS(7_0)    = 0x02,

NSUnderlineStyleDouble NS_ENUM_AVAILABLE_IOS(7_0)   = 0x09,

NSUnderlinePatternSolid NS_ENUM_AVAILABLE_IOS(7_0)      = 0x0000,

NSUnderlinePatternDot NS_ENUM_AVAILABLE_IOS(7_0)        = 0x0100,

NSUnderlinePatternDash NS_ENUM_AVAILABLE_IOS(7_0)       = 0x0200,

NSUnderlinePatternDashDot NS_ENUM_AVAILABLE_IOS(7_0)    = 0x0300,

NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE_IOS(7_0) = 0x0400,

NSUnderlineByWord NS_ENUM_AVAILABLE_IOS(7_0) = 0x8000

} NS_ENUM_AVAILABLE_IOS(6_0);

typedef NS_ENUM(NSInteger, NSTextWritingDirection) {

NSTextWritingDirectionEmbedding     = (0 << 1),

NSTextWritingDirectionOverride      = (1 << 1)

} NS_ENUM_AVAILABLE_IOS(7_0);

NSTextEffectLetterpressStyle NS_AVAILABLE_IOS(7_0);

从官方文档中我们可以看到,对于字符串我们很多的样式都可以设置,针对我们的需求我们可以挑选相应的设置来进行设置。但是需要提醒的是这些属性的设置只有在ios6.0以后才有用,所以我们在兼容性方面应该做好抉择。另外有些样式的设置是需要在ios7之后才可以使用。另外对于UILable我们可以使用setBackgroundColor,setFont,setTextColor等,但是在我们使用了NSMutableAttributedString 对应的属性将会被覆盖,所以我们在使用这些样式的时候应该要明白这一点。

NSMutableAttributedString/NSAttributedString 富文本设置的更多相关文章

  1. 【转】iOS使用NSMutableAttributedString实现富文本

    iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...

  2. 【代码笔记】iOS-获得富文本设置以后的文字高度

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  3. 【改】iOS学习之NSAttributedString(富文本)

    NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...

  4. iOS学习之NSAttributedString(富文本)

    NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...

  5. NSAttributedString富文本简单介绍和常用方法浅析

    NSAttributedString基本知识点介绍 1.初始化方法 - (instancetype)initWithString:(NSString *)str; - (instancetype)in ...

  6. iOS使用NSMutableAttributedString实现富文本小结

    NSAttributedString NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距).NSAttributedString对象的默 ...

  7. 关于NSMutableAttributedString进行富文本 UILabel 的制作

    //1.初始化与其他无异 NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithS ...

  8. swift---不同字体大小不同颜色label富文本设置

    agreeDeal = UILabel() //富文本,不同字体颜色大小和颜色 let labelString = "登录注册,表示您同意<服务条款及隐私政策>"as ...

  9. iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

随机推荐

  1. Ms sql将首字母大写

    --辅助表 create table a ( a int ) declare @b int begin insert into a values(@b) end; go --表数据 ),id int) ...

  2. maximo功能修改(初步理解)

    已接触IBM公司的MAXIMO近三个月,在这时间里自己对maximo也有所了解,今天将自己总结写在这里,方便自己的温习和大家的参考,不足之处还望指出,我一定在第一时间内修改. 今天在公司所做的就是完善 ...

  3. mgo中DBRef-数据添加测试

    2014-1-25 在设计mongo数据库时遇到这样一个问题,日志信息表需要引用人员信息表的数据.如果是结构化数据库,基本上不用想太多的东西.由于刚接触非结构化数据库,按着书上的理解由于日志数量较多, ...

  4. 关于delphi 中 Sender的学习

    sender是 事件的触发者,我发现所有的组件的事件 基本上都是 传Sender. 示例效果图: 代码: 接着来,既然TButton是个类,且publish哪里有事件,我们也可以看看这个事件的原型.

  5. Ninject的使用

    摘要 DI容器的一个责任是管理他创建的对象的生命周期.他应该决定什么时候创建一个给定类型的对象,什么时候使用已经存在的对象.他还需要在对象不需要的时候处理对象.Ninject在不同的情况下管理对象的生 ...

  6. mysql 主从复制原理

    主从形式   mysql主从复制 灵活 一主一从 主主复制 一主多从---扩展系统读取的性能,因为读是在从库读取的: 多主一从---5.7开始支持 联级复制---     用途及条件   mysql主 ...

  7. 在MS SQLSERVER中如何最快的速度清空所有用户表的数据

    有时候我们需要清空数据库中所有用户表的数据,如果一张表一张表的清空的话,遇到一个庞大的数据系统估计得崩溃了.  用游标加上用变量来引用表名就可以做到这一点. 用变量来引用表名对表操作可以用在存储过程中 ...

  8. nginx环境下配置nagios-关于nagios配置文件nginx.conf

    接上文:nginx环境下配置nagios-关于nginx.conf 配置如下: ;          location ~ .*\.(php|php5)?$          {            ...

  9. linux上的编译安装

    计算机运行的程序都是二进制的代码,那么我们所用的编程语言都是自然语言中的字符,那么就需要有一种机制来将这些转化成二进制代码,那么根据转化机制不一样,编程语言(软件 产生的源头)分两大类 解释型 编译型 ...

  10. 项目Windows服务安装命令:

    sc create YY.SmsPlatform.RemoteDataCenter binPath= "E:\YY.SmsPlatform\YY.SmsPlatform.RemoteData ...