如何设置UITextField的“padding”值
1个回答
2015-06-21 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517189
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
有时为了美化UITextField样式会使用自定义背景图片而将本身的边框隐藏,这时基本上光标的位置都不会很合适,而UITextField默认不可以设置padding,咱们可以自定义一个子类实现padding。
//
// UITextFieldEx.h
//
#import <Foundation/Foundation.h>
@interface UITextFieldEx : UITextField {
BOOL isEnablePadding;
float paddingLeft;
float paddingRight;
float paddingTop;
float paddingBottom;
}
- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left;
@end
// UITextFieldEx.m
#import "UITextFieldEx.h"
@implementation UITextFieldEx
- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left {
isEnablePadding = enable;
paddingTop = top;
paddingRight = right;
paddingBottom = bottom;
paddingLeft = left;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (isEnablePadding) {
return CGRectMake(bounds.origin.x + paddingLeft,
bounds.origin.y + paddingTop,
bounds.size.width - paddingRight, bounds.size.height - paddingBottom);
} else {
return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
}
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
调用方法:
UITextFieldEx *iptMsg = [[UITextFieldEx alloc]initWithFrame:CGRectMake(20, 25, 331, 46)];
[iptMsg setPadding:YES top:12 right:18 bottom:12 left:10];
iptMsg.borderStyle = UITextBorderStyleNone;
UIColor *iptMsgBg = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"chat_bottom_textfield.png"]];
iptMsg.backgroundColor = iptMsgBg;
[self.view addSubview:iptMsg];
//
// UITextFieldEx.h
//
#import <Foundation/Foundation.h>
@interface UITextFieldEx : UITextField {
BOOL isEnablePadding;
float paddingLeft;
float paddingRight;
float paddingTop;
float paddingBottom;
}
- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left;
@end
// UITextFieldEx.m
#import "UITextFieldEx.h"
@implementation UITextFieldEx
- (void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left {
isEnablePadding = enable;
paddingTop = top;
paddingRight = right;
paddingBottom = bottom;
paddingLeft = left;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (isEnablePadding) {
return CGRectMake(bounds.origin.x + paddingLeft,
bounds.origin.y + paddingTop,
bounds.size.width - paddingRight, bounds.size.height - paddingBottom);
} else {
return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
}
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
调用方法:
UITextFieldEx *iptMsg = [[UITextFieldEx alloc]initWithFrame:CGRectMake(20, 25, 331, 46)];
[iptMsg setPadding:YES top:12 right:18 bottom:12 left:10];
iptMsg.borderStyle = UITextBorderStyleNone;
UIColor *iptMsgBg = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"chat_bottom_textfield.png"]];
iptMsg.backgroundColor = iptMsgBg;
[self.view addSubview:iptMsg];
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询