先看个例子
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) 12 | NSFontAttributeName:[UIFont systemFontOfSize:20] |
NSParagraphStyleAttributeName(段落属性) | defaultParagraphStyle | |
NSForegroundColorAttributeName字体颜色 | blackColor | NSForegroundColorAttributeName:[UIColor redColor] |
NSBackgroundColorAttributeName背景颜色 | nil | NSBackgroundColorAttributeName:[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.0 | NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(3.0) |
NSShadowAttributeName字体阴影 | NSShadow类型, 默认为空(若有背景色, 阴影为背景色的阴影, 无背景色则为字体阴影) | NSShadow *shadow = [[NSShadow alloc] init];shadow.shadowOffset = CGSizeMake(10, 10);NSShadowAttributeName:shadow |
上面是iOS6.0 | 以下都是7.0后添加的 | |
NSTextEffectAttributeName特效(凹凸效果) | 默认nil | NSTextEffectAttributeName:NSTextEffectLetterpressStyle |
NSAttachmentAttributeName富文本时用到(以后研究) | 默认为空 | |
NSLinkAttributeName(添加链接) | NSURL (preferred) or NSString | |
NSBaselineOffsetAttributeName行间距 | 默认0 | NSBaselineOffsetAttributeName:@(10.0) |
NSUnderlineColorAttributeName下划线颜色 | UIColor, default nil: same as foreground color | NSUnderlineColorAttributeName:[UIColor blackColor] |
NSStrikethroughColorAttributeName(删除线颜色) | UIColor, default nil: same as foreground color | NSStrikethroughColorAttributeName:[UIColor greenColor] |
NSObliquenessAttributeName斜体字 | NSNumber, 默认0:倾斜 | NSObliquenessAttributeName:@(2.0) |
NSExpansionAttributeName文本扁平化 | NSNumber, 默认0 | NSExpansionAttributeName:@(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