NSAttributedString的使用 使label具有點擊事件 定義帶參數巨集的註意事項 pch文件基本使用規則 使用CocoaPods集成Masonry框架 Xcode7中VVDocument插件解決方案
註:通常的label用來現實普通的文字。但是,你常常會遇到這樣的情況:一段文字中不僅有文字,也有圖片,甚至文字中的某段文字與其他的文字的appearance不一致的情況,這樣的一段文字就可以稱得上是富文本了。label的attributedText屬性就是用來接受這樣的文本內容的。
場景
如圖
- 若你遇到這樣的需求,不妨考慮一下使用NSAttributedString了創建這樣的文本。如果這段文字具有點擊事件,實現方法有以下兩種:
- 將這段文字設置為button的attributedTitle
- 將這段文字設置為label的attributedText,並給label添加點擊手勢
- 若你遇到這樣的需求,不妨考慮一下使用NSAttributedString了創建這樣的文本。如果這段文字具有點擊事件,實現方法有以下兩種:
實現思路
- 這段文字由圖片和文字共同組成
- 將圖片封裝到NSTextAttachment實例中,然後通過NSAttributedString的類構造方法初始化為NSAttributedString實例。
- 使用NSMutableDictionary來封裝文本的現實屬性
- 使用NSAttributedString的對象方法addAttributes:range:改變指定範圍文字的現實屬性
具體實現
集成Masonry框架
- 創建pch文件
- pch文件通常的命名方法:項目名-prefix.pch,如:AttributedStringInLabel-Prefix.pch
pch文件的配置
將通用的頭文件添加到pch文件中
- 定義通過RGBA創建UIColor對象的巨集
- 我們通常會將經常使用的方法定義成巨集,來提高開發效率和屏蔽複雜操作
帶參數的巨集定義中的參數名,不能與其後的形式參數名相同(巨集定義其實就是替換,將文本替換成指定的文本)
// redValue 不能寫成red #define UIColorWithInt(redValue, greenValue, blueValue, alphaValue) [UIColor colorWithRed:(redValue)/255.0f green:(greenValue)/255.0f blue:(blueValue)/255.0f alpha:(alphaValue)]
- alertLabel
包含alertLabel屬性
@interface ViewController () /** alertLabel */ @property (nonatomic, strong) UILabel *alertLabel; @end
創建alertLabel
- (void)viewDidLoad { [super viewDidLoad]; // 創建alertLabel self.alertLabel = [[UILabel alloc] init]; [self.view addSubview:self.alertLabel]; // 設置alertLabel的富文本屬性 [self setupAlertLabel]; }
- 使用Masonry框架佈局alertLabel的位置
- 若是在控制器中,通常在viewDidLayoutSubviews方法中佈局子控制項
若是自定以控制,通常在layoutSubviews方法中佈局子控制項
/** * 佈局alertLabel */ - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.centerY.equalTo(self.view); }]; }
設置alertLabel的attributedText屬性
/** * 設置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]; }
- 給alertLabel添加點擊手勢
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"); }
VVDocument
- VVDocument是一款快速編寫註釋的Xcode插件,但是升級Xcode之後,出現了VVDocument不可用的情況,以下是解決方案
打開“Finder”->“應用程式”->“Xcode”->"顯示包內容"->"contents"->"Info.plist",拷貝如圖所示內容
command+shift+G,進入指定路徑文件夾:~/Library/Application Support/Developer/Shared/Xcode
“顯示包內容”->“Contents”->"Info.plist", 新建Item,粘貼拷貝的字元串
重啟Xcode,使用三個斜杠(///)來使用VVDocument