注:一般的label用來現實普通的文字。可是,你經常會遇到這樣的狀況:一段文字中不只有文字,也有圖片,甚至文字中的某段文字與其餘的文字的appearance不一致的狀況,這樣的一段文字就能夠稱得上是富文本了。label的attributedText屬性就是用來接受這樣的文本內容的。app
如圖框架
集成Masonry框架佈局
pch文件的配置atom
將通用的頭文件添加到pch文件中插件
帶參數的宏定義中的參數名,不能與其後的形式參數名相同(宏定義其實就是替換,將文本替換成指定的文本)3d
// redValue 不能寫成red #define UIColorWithInt(redValue, greenValue, blueValue, alphaValue) [UIColor colorWithRed:(redValue)/255.0f green:(greenValue)/255.0f blue:(blueValue)/255.0f alpha:(alphaValue)]
包含alertLabel屬性code
@interface ViewController () /** alertLabel */ @property (nonatomic, strong) UILabel *alertLabel; @end
建立alertLabelorm
- (void)viewDidLoad { [super viewDidLoad]; // 建立alertLabel self.alertLabel = [[UILabel alloc] init]; [self.view addSubview:self.alertLabel]; // 設置alertLabel的富文本屬性 [self setupAlertLabel]; }
如果自定以控制,一般在layoutSubviews方法中佈局子控件對象
/** * 佈局alertLabel */ - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.centerY.equalTo(self.view); }]; }
設置alertLabel的attributedText屬性blog
/** * 設置alertLabel */ - (void)setupAlertLabel { // 文本的顯示樣式 NSMutableDictionary *appearanceDictionary = [NSMutableDictionary dictionary]; appearanceDictionary[NSForegroundColorAttributeName] = UIColorWithInt(117, 117, 117, 1.0); appearanceDictionary[NSFontAttributeName] = [UIFont boldSystemFontOfSize:15]; // 文本內容(指定顯示屬性的文本) NSString *normolString = @" 莫將支付寶密碼告訴他人,謝謝!"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:normolString attributes:appearanceDictionary]; // 改變文本中某段文字的現實屬性 NSMutableDictionary *subAppearanceDictionary = [NSMutableDictionary dictionary]; subAppearanceDictionary[NSForegroundColorAttributeName] = [UIColor redColor]; subAppearanceDictionary[NSFontAttributeName] = [UIFont systemFontOfSize:17]; NSRange subRange = [normolString rangeOfString:@"謝謝!"]; [attributedString addAttributes:subAppearanceDictionary range:subRange]; // 添加圖片 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"alert"]; attachment.bounds = CGRectMake(0, 0, 14, 14); NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment]; [attributedString insertAttributedString:imageString atIndex:0]; // 設置alertLabel的attributedText self.alertLabel.attributedText = attributedString; // 給alertLabel添加點擊事件 [self addTargetToAlertLabel]; }
UILabel對象默認是不具有與用戶交互的能力,若要保證添加的手勢有效果,須要是其具有與用戶交互的能力
- (void)addTargetToAlertLabel { self.alertLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertLabelClick:)]; [self.alertLabel addGestureRecognizer:tap]; } /** * alertLabel的點擊事件 */ - (void)alertLabelClick:(UILabel *)label { NSLog(@"alertLabelClick"); }
打開「Finder」->「應用程序」->「Xcode」->"顯示包內容"->"contents"->"Info.plist",拷貝如圖所示內容
command+shift+G,進入指定路徑文件夾:~/Library/Application Support/Developer/Shared/Xcode
「顯示包內容」->「Contents」->"Info.plist", 新建Item,粘貼拷貝的字符串
重啓Xcode,使用三個斜槓(///)來使用VVDocument