NSAttributedString 小demo

昨天看standFord 2013的iOS开发的公开课看到的新东西,里面的老师说NSAttributedString 是iOS6的新东西,不过我看文档的时候发现很多方法在3.2的时候就有了,google了一下,最后在stack overflow看到答案http://stackoverflow.com/questions/13389647/is-nsattributedstring-available-before-ios-6

It has been around since iOS3.2. However the ability to use them within certain UI controls such as UILabel, UIButton, UITextView, etc is only available since iOS6. You can still use them using CoreText for < 6.0.

iOS6 之后加入到UIKit中。


首先,NSAttributedString并不是String.

在NSAttributedString中,有很多dictionary,每个都是一个attribute,NSAttributedString的作用,就是设置文字的效果(颜色、字体、下划线等等),以前可以使用CoreText来达到同样的效果,不过根据课程老师的描述,NSAttributedString效率非常快。关于CoreText,以后再介绍下。

当然,NSAttributedString还是有点像NSString的,就是他们的内容一旦设定就不可以改变,所以也就有NSMutableAttributedString。

下面是讲稿中的一页,可以看出在NSAttributedString中放的是什么




下面直接进入demo,更容易理解

界面的UI是这样的



这个项目的目的就是使用这些按钮来改变中间那段话中的被选中的文字的相关属性。

首先,因为要使用NSAttributedString 所以中间这个Label的文字属性需要更改,如下图所示,Text要选择Attributed

为了更直观,所以项目中并不是要把文字颜色或者是字体应用到这段文字中,只是应用到某个单词。

这里使用UIStepper来选择打次,setpper可以设置每次的步长(默认是1)。

所以使用stepper来每次往后往前选择一个单词,为了做到这点,首先就要把label中的这句话分成一个个的单词

使用下面这个方法,可以很容易的将他们分出来

- (NSArray *)wordList
{
    NSArray *wordList = [[self.label.attributedText string] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if([wordList count]) {
        return wordList;
    } else {
        return @[@""];
    }
}
为方便分别添加各种属性,所以写了下面两个方法以供调用

- (void)addLabelAttributes:(NSDictionary *)attributes range:(NSRange)range
{
    if(range.location != NSNotFound) {
        NSMutableAttributedString *mat = [self.label.attributedText mutableCopy];
        [mat addAttributes:attributes range:range];
        self.label.attributedText = mat;
    }
}

- (void)addSelectedWordAttributes:(NSDictionary *)attributes
{
    NSRange range = [[self.label.attributedText string] rangeOfString:self.selectedWordLabel.text];
    [self addLabelAttributes:attributes range:range];
}

从上面的代码可以看到,每次想像操作String一样操作attributedString的时候,需要先调用string方法,然后就可以调用NSString的各种方法啦。

因为label是NSAttributedString,所以内容是不可以改变的,要往里面添加东西,就得先做个mutableCopy,然后再在copy的里面进行添加,添加好后让原来的attributedString指向这个copy就ok了

剩下的操作就是添加各个属性而已 直接上源码咯

//
//  ViewController.m
//  AttributedStringDemo
//
//  Created by kay_sprint on 13-4-5.
//  Copyright (c) 2013年 cn.edu.scnu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIStepper *selectedWordStepper;
@property (weak, nonatomic) IBOutlet UILabel *selectedWordLabel;

@end

@implementation ViewController

- (void)addLabelAttributes:(NSDictionary *)attributes range:(NSRange)range
{
    if(range.location != NSNotFound) {
        NSMutableAttributedString *mat = [self.label.attributedText mutableCopy];
        [mat addAttributes:attributes range:range];
        self.label.attributedText = mat;
    }
}

- (void)addSelectedWordAttributes:(NSDictionary *)attributes
{
    NSRange range = [[self.label.attributedText string] rangeOfString:self.selectedWordLabel.text];
    [self addLabelAttributes:attributes range:range];
}

- (IBAction)underline
{
    [self addSelectedWordAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}];
}

- (IBAction)unUnderLine
{
    [self addSelectedWordAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone)}];
}

- (IBAction)changeSelectedWordColor:(UIButton *)sender {
    [self addSelectedWordAttributes:@{NSForegroundColorAttributeName:sender.backgroundColor}];
}

- (IBAction)changeFont:(UIButton *)sender {
    NSDictionary *attributes = [self.label.attributedText attributesAtIndex:0 effectiveRange:NULL];

    CGFloat fontSize = [UIFont systemFontSize];

    UIFont *existingFont = attributes[NSFontAttributeName];
    if(existingFont) fontSize = existingFont.pointSize;

    UIFont *font = [sender.titleLabel.font fontWithSize:fontSize];
    [self addSelectedWordAttributes:@{NSFontAttributeName:font}];
}

- (IBAction)outline {
    [self addSelectedWordAttributes:@{NSStrokeWidthAttributeName:@5}];
}

- (IBAction)unOutline {
    [self addSelectedWordAttributes:@{NSStrokeWidthAttributeName:@0}];
}


- (NSArray *)wordList
{
    NSArray *wordList = [[self.label.attributedText string] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if([wordList count]) {
        return wordList;
    } else {
        return @[@""];
    }
}

- (NSString *)selectedWord
{
    return [self wordList][(int)self.selectedWordStepper.value];
}

- (IBAction)updateSelectedWord {
    self.selectedWordStepper.maximumValue = [[self wordList] count]-1;
    self.selectedWordLabel.text = [self selectedWord];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self updateSelectedWord];
}

@end

来个效果图



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值