UILabel和NSAttributedString那些事

注:一般的label用來現實普通的文字。可是,你經常會遇到這樣的狀況:一段文字中不只有文字,也有圖片,甚至文字中的某段文字與其餘的文字的appearance不一致的狀況,這樣的一段文字就能夠稱得上是富文本了。label的attributedText屬性就是用來接受這樣的文本內容的。app

場景

  • 如圖框架

    • 若你遇到這樣的需求,不妨考慮一下使用NSAttributedString了建立這樣的文本。若是這段文字具備點擊事件,實現方法有如下兩種:
      • 將這段文字設置爲button的attributedTitle
      • 將這段文字設置爲label的attributedText,並給label添加點擊手勢

實現思路

  • 這段文字由圖片和文字共同組成
    • 將圖片封裝到NSTextAttachment實例中,而後經過NSAttributedString的類構造方法初始化爲NSAttributedString實例。
    • 使用NSMutableDictionary來封裝文本的現實屬性
    • 使用NSAttributedString的對象方法addAttributes:range:改變指定範圍文字的現實屬性

具體實現

  • 集成Masonry框架佈局

  • 建立pch文件
    • pch文件一般的命名方法:項目名-prefix.pch,如:AttributedStringInLabel-Prefix.pch
    • pch文件的配置atom

    • 將通用的頭文件添加到pch文件中插件

  • 定義經過RGBA建立UIColor對象的宏
    • 咱們一般會將常常使用的方法定義成宏,來提升開發效率和屏蔽複雜操做
    • 帶參數的宏定義中的參數名,不能與其後的形式參數名相同(宏定義其實就是替換,將文本替換成指定的文本)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
    • 包含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];
      }
    • 使用Masonry框架佈局alertLabel的位置
      • 如果在控制器中,一般在viewDidLayoutSubviews方法中佈局子控件
      • 如果自定以控制,一般在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];
      }
    • 給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

相關文章
相關標籤/搜索