UITextField的placeholder设置

1.UITextField的光标颜色

textField.tintColor = [UIColor whiteColor];

2.UITextField占位文字相关的设置

   2.1 设置占位文字内容

@property(nullable, nonatomic,copy)  NSString              *placeholder;

  2.2 设置带有属性的占位文字, 优先级 > placeholder

@property(nullable, nonatomic,copy)  NSAttributedString    *attributedPlaceholder;

3.NSAttributedString

   3.1 带有属性的字符串, 富文本

       -由2部分组成

          - 文字内容 : NSString *

          - 文字属性 : NSDictionary *

               - 文字颜色 - NSForegroundColorAttributeName

                 - 字体大小 - NSFontAttributeName

                 - 下划线 - NSUnderlineStyleAttributeName

                - 下划线颜色 - NSUnderlineColorAttributeName

                 - 背景色 - NSBackgroundColorAttributeName

  3.2 初始化

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];

attributes[NSForegroundColorAttributeName] = [UIColor grayColor];

attributes[NSBackgroundColorAttributeName] = [UIColor redColor];

NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attributes];

         使用场合

              - UILabel - attributedText

               - UITextField - attributedPlaceholder

4. NSMutableAttributedString

    4.1 NSMutableAttributedString继承自NSAttributedString

    4.2 常见方法

       4.21 设置range范围的属性, 重复设置同一个范围的属性, 最后一次设置才是有效的(之前的设置会被覆盖掉)

             - (void)setAttributes:(nullable NSDictionary*)attrs range:(NSRange)range;

      4.22 添加range范围的属性, 同一个范围, 可以不断累加属性

            - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

             - (void)addAttributes:(NSDictionary*)attrs range:(NSRange)range;

      4.3 例子如图

NSMutableAttributedString对UITextField的placeholder设置
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容