iPhone NSMutableAttributedString

選択した文字の色とフォントをボタンで変えて遊べるようなiPhoneアプリを描いてみます。(NSMutableAttributedStringを使ってみました。)


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

@interface ViewController () <UITextViewDelegate>

@property (weak, nonatomic) UITextView *textView;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createWords];

    [self createColorButtons];

    [self createFontButtons];

}

– (void)createWords

{

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@”Today is the first day.”];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”AvenirNext-Regular” size:70.0] range:NSMakeRange(0, 23)];

    

    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];

    textView.delegate = self;

    textView.attributedText = str;

    [self.view addSubview:textView];

    self.textView = textView;

    

    UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

    textView.inputView = dummyView;

    [UIMenuController sharedMenuController].menuVisible = NO;

}

– (void)createColorButtons

{

    NSArray *colors = @[[UIColor blackColor], [UIColor orangeColor], [UIColor purpleColor]];

    

    float x = 20;

    for (UIColor *color in colors) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(x, 410, 40, 40);

        btn.backgroundColor = color;

        [self.view addSubview:btn];

        [btn addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];

        

        x+= 50;

    }

    

}

– (void)changeColor:(UIButton*)sender

{

    NSMutableAttributedString *mat = [self.textView.attributedText mutableCopy];

    [mat addAttribute:NSForegroundColorAttributeName value:sender.backgroundColor range:self.textView.selectedRange];

    self.textView.attributedText = mat;

}

– (void)createFontButtons

{

    

    //AvenirNext-Heavy

    //AvenirNext-UltraLight

    //ChalkboardSE-Regular

    

    NSArray *fonts = @[[UIFont fontWithName:@”AvenirNext-Heavy” size:80],

                        [UIFont fontWithName:@”AvenirNext-UltraLight” size:80],

                        [UIFont fontWithName:@”ChalkboardSE-Regular” size:80],];

    

    float x = 160;

    for (UIFont *font in fonts) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(x, 480, 40, 40);

        btn.backgroundColor = [UIColor lightGrayColor];

        [btn setTitle:@”A” forState:UIControlStateNormal];

        btn.titleLabel.font = [font fontWithSize:40];

        [self.view addSubview:btn];

        [btn addTarget:self action:@selector(changeFont:) forControlEvents:UIControlEventTouchUpInside];

        

        x+= 50;

    }

}

– (void)changeFont:(UIButton*)sender

{

    NSMutableAttributedString *mat = [self.textView.attributedText mutableCopy];

    [mat addAttribute:NSFontAttributeName value:[sender.titleLabel.font fontWithSize:80] range:self.textView.selectedRange];

    

    self.textView.attributedText = mat;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end