一、 NSAttributedString
- 帶有屬性的字符串, 富文本
- 由2部分組成
- 文字內(nèi)容 : NSString *
- 文字屬性 : NSDictionary *
- 文字顏色 - NSForegroundColorAttributeName
- 字體大小 - NSFontAttributeName
- 下劃線 - NSUnderlineStyleAttributeName
- 背景色 - NSBackgroundColorAttributeName
- 初始化
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"123" attributes:attributes];
- 使用場合
- UILabel - attributedText
- UITextField - attributedPlaceholder
例如:
(label先進(jìn)行連線)
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.label.text attributes:attributes];
self.label.attributedText = string;
}
二廊敌、 NSMutableAttributedString
- 繼承自NSAttributedString
- 常見方法
// 設(shè)置range范圍的屬性, 重復(fù)設(shè)置同一個范圍的屬性, 最后一次設(shè)置才是有效的(之前的設(shè)置會被覆蓋掉)
- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
// 添加range范圍的屬性, 同一個范圍, 可以不斷累加屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
- 圖文混排
效果圖:
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100, 100, 200, 25);
label.backgroundColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:14];
[self.view addSubview:label];
// 圖文混排
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
// 1 - 你好
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
[attributedText appendAttributedString:first];
// 2 - 圖片
// 帶有圖片的附件對象
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"AppIcon29x29"];
CGFloat lineH = label.font.lineHeight;
attachment.bounds = CGRectMake(0,- ((label.frame.size.height - lineH) * 0.5 - 1), lineH, lineH);
// 將附件對象包裝成一個屬性文字
NSAttributedString *second = [NSAttributedString
![1.png](http://upload-images.jianshu.io/upload_images/3680843-f9a91ffeeb2da243.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)attributedStringWithAttachment:attachment];
[attributedText appendAttributedString:second];
// 3 - 哈哈哈
NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"];
[attributedText appendAttributedString:third];
label.attributedText = attributedText;
-
一個Label顯示多行不同字體的文字
效果圖:
UILabel *label = [[UILabel alloc] init];
// 設(shè)置屬性文字
NSString *text = @"你好\n哈哈哈";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
label.attributedText = attributedText;
// 其他設(shè)置
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(100, 100, 100, 40);
[self.view addSubview:label];
self.navigationItem.titleView = label;