NSAttributedString 属性

http://qiita.com/yusuga/items/30ee7a851aea0447db06 转自

ScreenShot

- (void)drawRect:(CGRect)rect
{    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"文字色\n背景色\n文字間隔\n取り消し線\n下線\n中抜き文字 ABCD\n影 ABCD"];

    /* フォント */
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont fontWithName:@"ChalkboardSE-Regular" size:30.f]
                    range:NSMakeRange(0, attrStr.length)];

    /* 文字色 */
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor blueColor]
                    range:foreRange];

    /* 背景色 */
    [attrStr addAttribute:NSBackgroundColorAttributeName
                    value:[UIColor greenColor]
                    range:backRange];

    /* カーニング(文字間隔) 0は無効でデフォルトは0 */
    [attrStr addAttribute:NSKernAttributeName
                    value:[NSNumber numberWithFloat:20.f]
                    range:kernRange];

    /* 取り消し線 */
    [attrStr addAttribute:NSStrikethroughStyleAttributeName
                    value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
                    range:strikeRange];

    /* 下線 */
    [attrStr addAttribute:NSUnderlineStyleAttributeName
                    value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
                    range:underRange];

    /* 中抜き文字(枠線色, 枠線幅を組み合わせ) */
    [attrStr addAttributes:@{NSStrokeColorAttributeName : [UIColor blueColor], NSStrokeWidthAttributeName : [NSNumber numberWithFloat:3.f]}
                     range:outlineRange];

    /* 影 */
    NSShadow *shadow = [[NSShadow alloc] init];
    // 影のサイズ
    shadow.shadowOffset = CGSizeMake(1.f, 1.f);
    // 影の色
    shadow.shadowColor = [UIColor redColor];
    // ぼかしの半径
    shadow.shadowBlurRadius = 5.f;
    [attrStr addAttribute:NSShadowAttributeName
                    value:shadow
                    range:shadowRange];

    /* 属性文字列の描画*/
    [attrStr drawInRect:rect];

    /* 描画に必要な矩形の取得 */
    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGRect drawnRect = [attrStr boundingRectWithSize:rect.size
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                             context:context];

    /* パラグラフスタイルの設定しての描画 */
    attrStr = [[NSMutableAttributedString alloc] initWithString:@"truncatingTail"];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    // 矩形が描画に必要なサイズより小さい場合に末尾を省略文字にする
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    [attrStr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:30.f], NSParagraphStyleAttributeName : style}
                     range:NSMakeRange(0, attrStr.length)];
    // 描画に必要なサイズを取得
    CGSize size = [attrStr boundingRectWithSize:rect.size
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                        context:context].size;
    // 横幅を減らして描画(つまり描画に必要なサイズが足りない)
    [attrStr drawInRect:CGRectMake(0.f, drawnRect.size.height, size.width - 10.f, size.height)];
}

/* UIButtonにNSAttributedStringを使用 */
NSString *title = @"BUTTON";
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(1.f, 1.f);
shadow.shadowColor = [UIColor blueColor];
shadow.shadowBlurRadius = 5.f;
NSDictionary *attr = @{NSStrokeColorAttributeName : [UIColor blueColor], NSStrokeWidthAttributeName : [NSNumber numberWithFloat:4.f]};
NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:title attributes:attr];
[self.button setAttributedTitle:attrTitle forState:UIControlStateNormal];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值