文字属性--Attributes

先看个例子

    NSDictionary *normalDict = @{
                                 NSFontAttributeName: [UIFont systemFontOfSize:15],
                                 NSForegroundColorAttributeName: [UIColor greenColor]
                                 };
    NSDictionary *selectDict = @{
                           NSFontAttributeName: [UIFont systemFontOfSize:18],
                           NSForegroundColorAttributeName: [UIColor redColor]
                           };

    //设置正常状态
    [segmentControl setTitleTextAttributes:normalDict forState:UIControlStateNormal];
    //设置选中状态
    [segmentControl setTitleTextAttributes:selectDict forState:UIControlStateSelected];
属性名默认值使用
NSFontAttributeName字体名, 字体大小Helvetica(Neue) 12NSFontAttributeName:[UIFont systemFontOfSize:20]
NSParagraphStyleAttributeName(段落属性)defaultParagraphStyle
NSForegroundColorAttributeName字体颜色blackColorNSForegroundColorAttributeName:[UIColor redColor]
NSBackgroundColorAttributeName背景颜色nilNSBackgroundColorAttributeName:[UIColor blackColor]
NSLigatureAttributeName是否连字default 1: 默认连字, 0: 不连字(经测试没什么效果)NSLigatureAttributeName:@(0)
NSKernAttributeName字间距含NSNumber的浮点值,修改默认的字距, 0表示禁用字距调整SKernAttributeName:@(30.5)
NSStrikethroughStyleAttributeName是否带删除线0:不带, 1:带NSStrikethroughStyleAttributeName:@(1)
NSUnderlineStyleAttributeName是否带下划线0:不带, 1:带NSUnderlineStyleAttributeName:@(1)
NSStrokeColorAttributeName空心字外层线条颜色UIColor, default nil: same as foreground color需要和NSStrokeWidthAttributeName配合使用
NSStrokeWidthAttributeName空心字外层线条宽度浮点型, 默认为0, 典型值3.0NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(3.0)
NSShadowAttributeName字体阴影NSShadow类型, 默认为空(若有背景色, 阴影为背景色的阴影, 无背景色则为字体阴影)NSShadow *shadow = [[NSShadow alloc] init];shadow.shadowOffset = CGSizeMake(10, 10);NSShadowAttributeName:shadow
上面是iOS6.0以下都是7.0后添加的
NSTextEffectAttributeName特效(凹凸效果)默认nilNSTextEffectAttributeName:NSTextEffectLetterpressStyle
NSAttachmentAttributeName富文本时用到(以后研究)默认为空
NSLinkAttributeName(添加链接)NSURL (preferred) or NSString
NSBaselineOffsetAttributeName行间距默认0NSBaselineOffsetAttributeName:@(10.0)
NSUnderlineColorAttributeName下划线颜色UIColor, default nil: same as foreground colorNSUnderlineColorAttributeName:[UIColor blackColor]
NSStrikethroughColorAttributeName(删除线颜色)UIColor, default nil: same as foreground colorNSStrikethroughColorAttributeName:[UIColor greenColor]
NSObliquenessAttributeName斜体字NSNumber, 默认0:倾斜NSObliquenessAttributeName:@(2.0)
NSExpansionAttributeName文本扁平化NSNumber, 默认0NSExpansionAttributeName:@(0.1)
NSWritingDirectionAttributeName设置文字书写方向,从左向右书写或者从右向左书写四种
NSVerticalGlyphFormAttributeName排版0 表示横排文本,1 表示竖排文本(经测试iOS中无竖排)

1.增加了链接

给UITextView增加了链接
现在在iOS添加你自己的Twitter账户更加简单了,现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action。

首先,创建一个NSAttributedString然后增加给它增加一个NSLinkAttributeName 属性,见以下:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"]; 
[attributedString addAttribute:NSLinkAttributeName 
                         value:@"username://marcelofabri_" 
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]]; 


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], 
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor], 
                                 NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; 

// assume that textView is a UITextView previously created (either by code or Interface Builder) 
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links 
textView.attributedText = attributedString; 
textView.delegate = self; 

这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    if ([[URL scheme] isEqualToString:@"username"]) { 
        NSString *username = [URL host];  
        // do something with this username 
        // ... 
        return NO; 
    } 
    return YES; // let the system open this URL 
} 

2.文字书写方向

LRE: NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding,
RLE: NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding,
LRO: NSWritingDirectionLeftToRight | NSWritingDirectionOverride,
RLO: NSWritingDirectionRightToLeft | NSWritingDirectionOverride,

3.段落属性

NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;//增加行高
style.headIndent = 10;//头部缩进,相当于左padding
style.tailIndent = -10;//相当于右padding
style.lineHeightMultiple = 1.5;//行间距是多少倍
style.alignment = NSTextAlignmentLeft;//对齐方式
style.firstLineHeadIndent = 20;//首行头缩进
style.paragraphSpacing = 10;//段落后面的间距
style.paragraphSpacingBefore = 20;//段落之前的间距
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];

可以参考(不过有的已经废弃了):http://www.itnose.net/detail/6177538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值