UISearchBar 属性字符串-占位符
需求需要,UISearchBar的占位符某一位开始之后是红色:
解决思路
- 查看UISearchBar的图层结构
看到里面有属于UITextField的子类 -UISearchBarTextField -,可以推断出其同样含有一个属性 - attributedPlaceholder -
核心代码如下
//.h
#import <UIKit/UIKit.h>
@interface CXSearchBar : UISearchBar
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder;
@end
//.m
#import "CXSearchBar.h"
@implementation CXSearchBar
- (void)layoutSubviews {
UITextField * searchField = [self valueForKey:@"_searchField"];
searchField.attributedPlaceholder = _attributedPlaceholder;
[super layoutSubviews];
}
@end