iPhone 千社札

入力した文字で千社札を作るiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, weak) UILabel *fadaLabelOutLine;

@property (nonatomic, weak) UILabel *fadaLabel;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    [self createFudaView];

    

    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(30, 30, 260, 30)];

    field.borderStyle = UITextBorderStyleRoundedRect;

    field.delegate = self;

    [self.view addSubview:field];

    

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

    title.text = @”千社札;

    title.font = [UIFont systemFontOfSize:40];

    [title sizeToFit];

    title.center = CGPointMake(70, 450);

    title.textColor = [UIColor colorWithWhite:0.9 alpha:1];

    [self.view addSubview:title];

}

– (void)createFudaView

{

    float w = 100.0;

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, w * 3.2)];

    v.backgroundColor = [UIColor whiteColor];

    v.center = CGPointMake(160, 250);

    [self.view addSubview:v];

    CALayer *border = [CALayer layer];

    border.frame = CGRectInset(v.bounds, 5, 5);

    border.borderColor = [UIColor colorWithWhite:0.25 alpha:1].CGColor;

    border.borderWidth = 4;

    border.shouldRasterize = YES;

    [v.layer addSublayer:border];

    

    // gradient

    CAGradientLayer *gradient = [CAGradientLayer layer];

    gradient.frame = CGRectInset(border.frame, 4, 4);

    gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor orangeColor].CGColor];

    gradient.locations = @[@(0.3), @(1)];

    [v.layer addSublayer:gradient];

    

    UILabel *l = [[UILabel alloc] initWithFrame:CGRectInset(gradient.frame, 10, 0)];

    l.backgroundColor = [UIColor clearColor];

    l.numberOfLines = 0;

    [v addSubview:l];

    self.fadaLabel = l;

    

    UILabel *outline = [[UILabel alloc] initWithFrame:CGRectInset(gradient.frame, 10, 0)];

    outline.backgroundColor = [UIColor clearColor];

    outline.numberOfLines = 0;

    [v addSubview:outline];

    self.fadaLabelOutLine = outline;

    

    v.transform = CGAffineTransformMakeRotation(-M_PI / 30.0);

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    

    NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:textField.text];

    [atts addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:60.0]} range:NSMakeRange(0, atts.length)];

    self.fadaLabel.attributedText = atts;

    

    NSMutableAttributedString *attsOutline = [[NSMutableAttributedString alloc] initWithString:textField.text];

    [attsOutline addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:60.0], NSStrokeWidthAttributeName : @2, NSStrokeColorAttributeName : [UIColor whiteColor] } range:NSMakeRange(0, attsOutline.length)];

    self.fadaLabelOutLine.attributedText = attsOutline;

    

    return YES;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end