自定义searchBar使其placeholder左对齐
来源:互联网 发布:通联数据股份有限公司 编辑:程序博客网 时间:2024/04/30 02:43
直接使用UIKit框架提供的UISearchBar来解决placeholder左对齐的问题(默认为居中对齐)貌似不太可能,没有找到相应的接口,所以采用自定义的方式来个性化。UISearchBar可以改成UITextField+UIButton来实现。UISearchBarDelegate中的方法也可以用相应的UITextFieldDelegate中的方法替代。
要想改变UITextField中的placeholder的对齐方式,需要修改:
-(void)drawPlaceholderInRect:(CGRect)rect对于这个函数需要注意的是,无法直接调用需要使用重载(也就是自定义继承的方式)来设置placeholder所在的rect的位置。
如下:HFMyTextField.m中添加如下代码。
-(void)drawPlaceholderInRect:(CGRect)rect{ CGFloat placeHolderRectX=rect.origin.x; CGFloat placeHolderRectY=(rect.size.height-HFSearchTextFieldPlaceholderFont.lineHeight)/2-1; CGFloat placeHolderRectW=rect.size.width; CGFloat placeHolderRectH=rect.size.height; rect=CGRectMake(placeHolderRectX, placeHolderRectY, placeHolderRectW, placeHolderRectH); [super drawPlaceholderInRect:rect];}如果需要动态修改placeholder的颜色、字体大小等,需要使用attributedPlaceholder属性
HFMyTextField* _recommendTitleSearchTextField=[[HFMyTextField alloc]init] NSDictionary* dict=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor],NSForegroundColorAttributeName,HFSearchTextFieldPlaceholderFont,NSFontAttributeName,nil]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:HFInitSearchFieldPlaceholderString attributes:dict]; _recommendTitleSearchTextField.attributedPlaceholder=attrString;不过有一点问题就是如果在HFMyTextField中的drawPlaceholderInRect方法中直接设置了placeholder的颜色属性,之后再使用attributedPlaceholder貌似并不能动态修改placeholder的颜色。代码如下
-(void)drawPlaceholderInRect:(CGRect)rect{ CGFloat placeHolderRectX=rect.origin.x; CGFloat placeHolderRectY=(rect.size.height-HFSearchTextFieldPlaceholderFont.lineHeight)/2-1; CGFloat placeHolderRectW=rect.size.width; CGFloat placeHolderRectH=rect.size.height; rect=CGRectMake(placeHolderRectX, placeHolderRectY, placeHolderRectW, placeHolderRectH); NSDictionary* attr=[NSDictionary dictionaryWithObjectsAndKeys:HFSearchTextFieldPlaceholderFont,NSFontAttributeName,[UIColor lightGrayColor],NSForegroundColorAttributeName ,nil]; self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:@""attributes:attr]; [super drawPlaceholderInRect:rect];}
0 0
- 自定义searchBar使其placeholder左对齐
- UISearchBar placeholder左对齐
- ios中UISearchBar的placeHolder左对齐
- 自定义searchBar
- 自定义Cell,使其左滑显示删除和分享
- Java_Swing图形界面-1选项卡的创建-2使其靠左垂直对齐
- 自定义alert文本内容左对齐
- 自定义SearchBar输入颜色
- iOS自定义searchBar
- 自定义导航searchBar
- iOS自定义searchBar
- EasySwift/EasySearchBar 自定义SearchBar
- iOS之自定义searchBar
- iOS自定义SearchBar样式
- 关于searchBar放大镜居左的设置
- Android 自定义 TextView drawableTop 图标与文字左对齐(效果图)
- 自定义searchBar 的一些设置
- Latex使\section中的内容左对齐
- matlab矩阵操作
- apache安装
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系
- 腾讯后台研发实习岗二面
- Android studio 使用记录(持续更新)
- 自定义searchBar使其placeholder左对齐
- LeetCode 238 Product of Array Except Self
- C#中的委托和事件
- C++ primer 第四章--数组和指针
- Js Map 和 List的简单实现
- 微信自定义分享的签名版实现
- mysql、sql server、oracle的连接信息
- ARC和非ARC下使用Block属性的问题
- Objective-C学习笔记1