UILabel和NSAttributedString那些事


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

場景

  • 如圖

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

實現思路

  • 這段文字由圖片和文字共同組成
    • 將圖片封裝到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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM