使用NSAttributeString创建样式丰富的文字

出现的问题

我们希望在UI组件中使用的文字有丰富多彩的样式,但是我们不希望为了丰富样式而将每一个文字都分割开来分别定义样式,更不希望先将文字图片话处理,然后加入到组件中,这样会占用更多的内存空间,对于iPhone等移动设备来说,这样做不科学,也不合理!比如说要按如下样式显示一个字符串。

多样式的字符串显示

多样式的字符串显示

如何解决问题

其实这个问题在OS X的系统中早已经解决掉了,针对一个NSString对象,我们可以为其中的文字定义不同的样式。但在iOS系统中,直到iOS 6才出现这个功能,所以使用这个功能时必须要考虑到系统的兼容性,如果希望兼容iOS 6之前的系统就得要重新考虑了。

具体做法就是使用NSAttributedString对象将普通的NSString对象属性话,当然这个对象是不可变的,其对应的可变对象是NSMutableAttributedString。

NSAttributedString对象可以通过initWithString:方法进行初始化,由于这个对象不可变,所以我们为了能更好的定义样式,通常使用其可变对象NSMutableString,初始化方法可能initWithString:是在NSAttributedString (NSExtendedAttributedString)中定义的。

然后可以通过创建属性字典NSDictionary,来定义属性,并用NSMutableAttributedString对象的- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;方法来为传入的NSString对象中的不同文字定义不同的样式。

如何创建属性字典

上面说了,我们需要定义一个属性的字典为不同的样式。这个可以通过API文档查询NSAttributedString看到有哪些属性是可以定义的。这里就说说上面效果需要用到的几个属性:

NSFontAttributeName:通过传入一个UIFont对象来定义文字的字体;

NSForegroundColorAttributeName:通过传入一个UIColor对象来定义文字的颜色;

NSBackgroundColorAttributeName:通过传入一个UIColor对象来定义文字的背景颜色;

NSShadowAttributeName:通过传入一个NSShadow来定义文字的阴影。

实现程序

下面代码就是上面效果的实现,通过定义一个label,然后设置其内容为一个带样式的文字对象即可。

//
//  ViewController.m
//  ConstructingDisplayingStyleText
//
//  Created by Mr. Right on 13-12-4.
//  Copyright (c) 2013年 XCoder Studio. All rights reserved.
//  XCoder Studio Website Http://www.xcoder.cn
//
 
#import "ViewController.h"
 
@interface ViewController ()
 
@property (nonatomic, strong) UILabel * label;
 
- (NSAttributedString *)attributedText;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    self.label = [[UILabel alloc] init];
    self.label.backgroundColor = [UIColor clearColor];
 
    // 定义UILabel的含有样式属性的文字内容
    self.label.attributedText = [self attributedText];
    // 定义UILabel的大小,使其可以容纳得下显示内容
    [self.label sizeToFit];
    self.label.center = self.view.center;
    [self.view addSubview:self.label];
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
// 获取带有不同样式的文字内容
- (NSAttributedString *)attributedText {
    // 定义要显示的文字内容
    NSString * string = @"XCoder Studio";
 
    // 通过要显示的文字内容来创建一个带属性样式的字符串对象
    NSMutableAttributedString * result = [[NSMutableAttributedString alloc] initWithString:string];
 
    // 定义第一个单词“XCoder”的文字样式字典
    NSDictionary * attributesForFirstWord = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:40.0f],
                                              NSForegroundColorAttributeName: [UIColor redColor],
                                              NSBackgroundColorAttributeName: [UIColor blackColor]};
    // 定义第二个单词“Studio”的阴影样式
    NSShadow * shadow = [[NSShadow alloc] init];
    // 定义阴影的颜色
    shadow.shadowColor = [UIColor darkGrayColor];
    // 定义阴影的偏移位置
    shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);
 
    // 定义第二个单词“Studio”的文字样式字典
    NSDictionary * attributesForSecondWord = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:40.0f],
                                               NSForegroundColorAttributeName: [UIColor whiteColor],
                                               NSBackgroundColorAttributeName: [UIColor redColor],
                                               NSShadowAttributeName: shadow};
    // 设置文字样式
    [result setAttributes:attributesForFirstWord range:[string rangeOfString:@"XCoder"]];
    [result setAttributes:attributesForSecondWord range:[string rangeOfString:@"Studio"]];
 
    // 返回已经设置好了的带有样式的文字
    return [[NSAttributedString alloc] initWithAttributedString:result];
}
 
@end

编译完成后就可以得到上述效果了!



转载:http://www.xcoder.cn/index.php/archives/667

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值