スワロフスキーを並べて作った文字が
表示できたら味があるかもと思ったので、
キーボードで入力した文字を水玉模様にしてみます。

環境
XcodeのiOS6 iPhone Simulatorで動かしています。

ポイント
UILabelのlayerから、Bitmapデータを取得して、
文字が描画されている部分、つまり黒の部分に
新しいUIViewの小さい丸を置いていくことで
ドット柄の文字を作る。




サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () <UITextFieldDelegate> 

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor purpleColor];

    

    [self createUI];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    

    for (int i=0; i<[self.view.subviews count]; i++) {

        UIView *v = [self.view.subviews objectAtIndex:i];

        if ([v isKindOfClass:[UITextField class]]) {

            [v removeFromSuperview];

        }

        float time = [self.view.subviews count] * 0.001;

        [self performSelector:@selector(twinkle:) withObject:v afterDelay:time – i * 0.001];

    }

}

– (void)twinkle:(UIView*)v

{

    [UIView animateWithDuration:0.1 animations:^{

        v.alpha = 0.5;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.1 animations:^{

            v.alpha = 1.0;

        }];

    }];

}

– (void)createUI

{

    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(50, 400, 220, 40)];

    field.borderStyle = UITextBorderStyleRoundedRect;

    field.delegate = self;

    [self.view addSubview:field];

}

– (void)createDotLabel:(UIView*)label

{

    int width = label.frame.size.width;

    int height = label.frame.size.height;

    

    UIGraphicsBeginImageContext(label.bounds.size);

    unsigned char *rawData = malloc(height * width * 4);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bitmapContext = CGBitmapContextCreate(rawData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast);

    [label.layer renderInContext:bitmapContext];

    

    for (int i=0; i<height*width*4; i+=4) {

        int x = (i/4) % width;

        int y = (i/4) / width;

        if (x % 4 == 0 && y % 4 == 0) {

            if (rawData[i] + rawData[i+1] + rawData[i+2] < 10) {

                CGRect r = CGRectMake(x – 1.8, height – y – 1.8, 3.6, 3.6);

                UIView *v = [[UIView alloc] initWithFrame:r];

                v.layer.cornerRadius = 1.8;

                v.backgroundColor = [UIColor yellowColor];

                [self.view addSubview:v];

            }

        }

    }

    

    CGColorSpaceRelease(colorSpace);

    free(rawData);

    UIGraphicsEndImageContext();

}

#define KEYBORD_HEIGHT 220

– (void)textFieldDidBeginEditing:(UITextField *)textField

{

    [self.view addSubview:textField];

    

    [UIView animateWithDuration:0.2 animations:^{

        textField.transform = CGAffineTransformMakeTranslation(0, – KEYBORD_HEIGHT);

    }];

    

    [UIView animateWithDuration:1.0 animations:^{

        for (UIView *v in self.view.subviews) {

            if ([v isKindOfClass:[UITextField class]]) {

                continue;

            }

            v.center = CGPointMake(v.center.x, v.center.y + 120);

        }

    }];

}

– (void)textFieldDidEndEditing:(UITextField *)textField

{

    [UIView animateWithDuration:0.2 animations:^{

        textField.transform = CGAffineTransformIdentity;

    }];

    

    UILabel *l = [[UILabel alloc] init];

    l.text = textField.text;

    l.font = [UIFont fontWithName:@”GillSans-Bold” size:80];

    [l sizeToFit];

    l.frame = CGRectMake(0, 0, l.frame.size.width, l.frame.size.height);

    l.backgroundColor = [UIColor redColor];

    

    [self createDotLabel:l];

    

    textField.text = @””;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end