1 NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:totalFee]; 2 [mutableString addAttribute:NSFontAttributeName 3 value:[UIFont systemFontOfSize:21.0] 4 range:NSMakeRange(3, totalFee.length - 3)];//設置字體 5 [mutableString addAttribute:NSForegroundColorAttributeName 6 value:[UIColor redColor] 7 range:NSMakeRange(3, totalFee.length - 3)];//設置顏色 8 [mutableString addAttribute:NSUnderlineStyleAttributeName 9 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] 10 range:NSMakeRange(0, totalFee.length)];//添加下划線 11 sumLabel.attributedText = mutableString;
NSAttributedString的初始化方法有:
-initWithString:用String初始化,並沒有Attributed信息。
-initWithAttributedString:用AttributedString去初始化。
-initWithString:Attributed:用string及attribute的dictionary來初始化。
具體AttributtedString屬性的鍵值對如下:
1、NSString *const NSFontAttributeName;//值為UIFont,設置字體,默認值為12-point Helvetica(Neue) 。
下面這段代碼可以查看ios中可用的字體,具體那些字體長什么樣,可以查看字體冊工具。
1 NSArray *familyArray = [UIFont familyNames]; 2 for (id family in familyArray) { 3 printf(“%s\n”,[family cStringUsingEncoding:NSUTF8StringEncoding]); 4 NSArray *fontArray = [UIFont fontNamesForFamilyName:family]; 5 for (id font in fontArray) { 6 printf(” %s\n”,[font cStringUsingEncoding:NSUTF8StringEncoding]); 7 } 8 }
2、NSString *const NSParagraphStyleAttributeName;//值為NSParagraphStyle,設置段落屬性,默認值為[NSParagraphStyle defaultParagraphStyle]返回的值。
NSMutableParagraphStyle與NSParagraphStyle包括以下屬性:
alignment //對齊方式
firstLineHeadIndent //首行縮進
headIndent //縮進
tailIndent //尾部縮進
lineBreakMode //斷行方式
maximumLineHeight //最大行高
minimumLineHeight //最低行高
lineSpacing //行距
paragraphSpacing //段距
paragraphSpacingBefore //段首空間
baseWritingDirection //句子方向
lineHeightMultiple //可變行高,乘因數。
hyphenationFactor //連字符屬性
3、NSString *const NSForegroundColorAttributeName;//值為UIColor,字體顏色,默認為黑色。
4、NSString *const NSBackgroundColorAttributeName;//值為UIColor,字體背景色,默認沒有。
5、NSString *const NSLigatureAttributeName;//值為整型NSNumber,連字屬性,一般中文用不到,在英文中可能出現相鄰字母連筆的情況。0為不連筆;1為默認連筆,也是默認值;2在ios 上不支持。
6、NSString *const NSKernAttributeName;//值為浮點數NSNumber,字距屬性,默認值為0。
7、NSString *const NSStrikethroughStyleAttributeName;//值為整型NSNumber,可取值為
enum {
NSUnderlineStyleNone = 0×00,
NSUnderlineStyleSingle = 0×01,
};設置刪除線。
8、NSString *const NSUnderlineStyleAttributeName;//同上。設置下划線。
9、NSString *const NSStrokeColorAttributeName;//值為UIColor,默認值為nil,設置的屬性同ForegroundColor。
10、NSString *const NSStrokeWidthAttributeName;//值為浮點數NSNumber。設置比畫的粗細。
11、NSString *const NSShadowAttributeName;//值為NSShadow,設置比畫的陰影,默認值為nil。
12、NSString *const NSVerticalGlyphFormAttributeName;//值為整型NSNumber,0為水平排版的字,1為垂直排版的字。
