iOS開発のNSAttributedString使用

8775 ワード

本文はNSAttributedStringとNSMutableAttributedStringの簡単な使い方を紹介した.
一.NSAttributedStringの紹介
  • NSAttributedString.hファイル
  • @interface NSAttributedString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>
  • それは2つの部分からなる.文字内容:NSString *.テキスト属性:NSDictionary *
  •      - NSForegroundColorAttributeName
         - NSFontAttributeName
        - NSUnderlineStyleAttributeName
        - NSBackgroundColorAttributeName

    二.NSMutableAttributedStringの紹介
  • NSAttributedString.hファイル
  • @interface NSMutableAttributedString : NSAttributedString
  • NSMutableAttributedStringでよく使われる方法は3つあります.1.range範囲の属性を設定し、同じ範囲の属性を繰り返し設定し、後の設定で前の設定が上書きされます.
  •   - (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs   range:(NSRange)range;

    2.range範囲の属性を追加し、同じ範囲で、属性を絶えず追加することができる.
      - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

    3.一度に1つの範囲内の複数の属性を追加する.
      - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;

    三.需要
  • テキストボックスには、プレースホルダ文字のフォント色、背景色および下線が設定.xibまたはstoryboardで作成するインタフェースは、インタフェースの右側に対応する設定属性が見つからない.

  • 四.解決する
  • 方法1.NSAttributedStringにより、UItextFieldに継承するクラスをカスタマイズし、awakeFromNibメソッドに以下のコードを書く.
  • NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
    attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
    attributes[NSUnderlineStyleAttributeName] = @YES;
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"o  o" attributes:attributes];

    効果図は以下の通りです.
  • 注意:UItextFieldのClassを図のように指定することを忘れないでください:
  • 方法2:NSMutableAttributedStringにより実現する.コードは次のとおりです:
  • NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"o  o"];
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
    attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
    attributes[NSUnderlineStyleAttributeName] = @YES;
    [string setAttributes:attributes range:NSMakeRange(0, 4)];
    self.attributedPlaceholder = string;
  • メソッド3:
  • NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"o  o"];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 4)];
    [string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
    [string addAttribute:NSUnderlineStyleAttributeName value:@YES range:NSMakeRange(0, 4)];
    self.attributedPlaceholder = string;
  • メソッド4:drawPlaceholderInRectメソッド
  • を書き換える
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
    attributes[NSFontAttributeName] = self.font;
    attributes[NSUnderlineStyleAttributeName] = @YES;
    CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);
    [self.placeholder drawAtPoint:placeholderPoint withAttributes:attributes];
  • 方法5:ビュー階層化により、UItextFieldにはUItextFieldLabelが含むことがわかる.
  • で占有ビューは何で表示されますか?self.subviews.lastObject.classによる、ビットマップはUItextFieldLabelによって表示することが分かる、self.subviews.lastObject.superClassによってUItextFieldLabelの親はUILabelであることが分かるので、.textColorの方法で文字色を表示することができる.しかし、self.subviews.lastObject.classの方法で取るものが必ずUItextFieldLabelであるとは保証できない.だから運行時に登場した.
  • UITextFieldLabelはUITextField.hヘッダファイルには具体的な内容は見つかりませんが、簡単な@class宣言なので、実行時にUItextFieldのメンバー変数や属性を確認する必要があります.その結果、placeholderLabelというやつを見つけて嬉しいです.placeholderLabelがUItextFieldLabelに対応する内容を指していると理解できますので、プレースビューもplaceholderLabelです!!では、.textColorで色を設定もよい.
  • unsigned int count;
    Ivar *ivarList = class_copyIvarList([UITextField class], &count);
    for (int i = 0; i < count; i++)
    {
        Ivar ivar = ivarList[i];
        NSString *str = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSLog(@"%@", str);
        }
    free(ivarList);
  • KVCで最終的に得られた:
  • static NSString * const kPlaceholderColorKey = @"placeholderLabel.textColor";
    static NSString * const kPlaceholderBGColorKey = @"placeholderLabel.backgroundColor";
    [self setValue:[UIColor yellowColor] forKeyPath:kPlaceholderColorKey];
    [self setValue:[UIColor redColor] forKeyPath:kPlaceholderBGColorKey];

    下線の設定はKCVではできませんが、必要でなければ1つ目の書き方に戻ります
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSUnderlineStyleAttributeName] = @YES;
    NSAttributedString *attributeText = [[NSAttributedString alloc] initWithString:@"o  o" attributes:attributes];
    [self setValue:attributeText forKeyPath:kPlaceholderUnderLineKey];
  • 説明:UItextFieldLabelの親はUILabelである.UILabelにはTextColor属性があるが、UILabelはUIViewから継承する、UIViewにはbackgroundColor属性がある.したがって、UItextFieldLabelは文字の色と背景の色を設定することができる.placeholderLabelはプログラム内部のプライベート属性であり、UItextFieldLabelの内容を指すため、文字色や背景色を設定することもできる.

  • 簡書
    iOS開発のNSAttributedString使用
    個人ブログ
    iOS開発のNSAttributedString使用
    GitHub
    NSAttributedStringDemo