1、常見的屬性及說明
NSFontAttributeName //字體
NSParagraphStyleAttributeName //段落格式
NSForegroundColorAttributeName //字體顏色
NSBackgroundColorAttributeName //背景顏色
NSStrikethroughStyleAttributeName //刪除線格式
NSUnderlineStyleAttributeName //下劃線格式
NSStrokeColorAttributeName //刪除線顏色
NSStrokeWidthAttributeName //刪除線寬度
NSShadowAttributeName //陰影
2、常見方法:
//為某一范圍內文字設置多個屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
//為某一范圍內文字添加某個屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//為某一范圍內文字添加多個屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
//移除某范圍內的某個屬性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
更多方法和屬性說明詳見蘋果官方說明文檔。
3、使用示例:
NSString *str = @"犯我中華者,雖遠必誅!";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
initWithString:str];
/*說明:NSAttributedString也能設置,與NSMutableAttributedString的關系類似于NSArray和NSMutableArray*/
(1)、添加字體和設置字體的范圍
[attrStr addAttribute:NSFontAttributeName value:
[UIFont systemFontOfSize:20.0f] range:NSMakeRange(0, 3)]; //字體大小為20.0f
[attrStr addAttribute:NSFontAttributeName value:
[UIFont boldSystemFontOfSize:20.0f] range:NSMakeRange(0, 3)]; //字體大小為20.0f并且加粗
(2)、添加文字顏色
[attrStr addAttribute:NSForegroundColorAttributeName value:
[UIColor redColor] range:NSMakeRange(0, 7)];
(3)、添加下劃線
[attrStr addAttribute:NSUnderlineStyleAttributeName value:
[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 7)];
(4)、設置段落樣式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//行間距
paragraph.lineSpacing = 10;
//段落間距
paragraph.paragraphSpacing = 20;
//對齊方式
paragraph.alignment = NSTextAlignmentLeft;
//指定段落開始的縮進像素
paragraph.firstLineHeadIndent = 30;
//調整全部文字的縮進像素paragraph.headIndent = 10;
(5)、添加段落設置
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraph
range:NSMakeRange(0, [str length])];
(6)、添加鏈接
label添加鏈接注意:label鏈接是可以顯示出來,但是不能點擊,而textView是可以點擊的,因為里面有shouldInteractWithURL代理方法回調。
NSString *urlStr = @"www.baidu.com";
NSURL *url = [NSURL URLWithString:urlStr];
[attrStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(2, 7)];
(7)、一次性搞定:設字號為20,字體顏色為紅色
NSDictionary *attDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:20.0],NSFontAttributeName,
[UIColor redColor],NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]
initWithString:@"犯我華夏者,雖遠必誅!" attributes:attDict];
4、label其他一些常用屬性:
//創建label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 0)];
//設置背景顏色
label.backgroundColor = [UIColor lightGrayColor];
//自動換行
label.numberOfLines = 0;
//設置label的富文本
label.attributedText = attrStr;
//label高度自適應
[label sizeToFit];
//打印高度
CGFloat height = label.frame.size.height;
NSLog(@"height = %f",height);
**
PS:設置sizeToFit之后是可以取出label的高度的,這樣做label高度自適應。但是如果你用第三方框架(如:Masonry)給其加約束,因為約束優先級最高,所以這句會失效
**
5、設置行間距
NSString *textStr = @":設置sizeToFit之后是可以取出label的高度的,這樣做label高度自適應。但是如果你用第三方框架(如:Masonry)給其加約束,因為約束優先級最高,所以這句會失效";
UIFont *textFont = [UIFont systemFontOfSize:14];
CGSize textSize = [textStr sizeWithFont:textFont
constrainedToSize:CGSizeMake(bounds.size.width - 40, QZONE_SCREEN_HEIGHT)];;
UILabel *openMicPrivilegeTipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, textSize.width, textSize.height)];
openMicPrivilegeTipsLabel.textColor = DefaultDescriptionText2ColorInDefaultTheme;
openMicPrivilegeTipsLabel.text = textStr;
openMicPrivilegeTipsLabel.backgroundColor = [UIColor clearColor];
openMicPrivilegeTipsLabel.textAlignment = UITextAlignmentLeft;
openMicPrivilegeTipsLabel.font = [UIFont systemFontOfSize:14];
openMicPrivilegeTipsLabel.numberOfLines = 0;
// 調整行間距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];
openMicPrivilegeTipsLabel.attributedText = attributedString;
[_tipsBG addSubview:openMicPrivilegeTipsLabel];
[openMicPrivilegeTipsLabel sizeToFit];