我将UITextField
:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface TWAdjustmentsTextField : UITextField
@property (nonatomic) IBInspectable NSInteger topInset;
@property (nonatomic) IBInspectable NSInteger leftInset;
@end
#import "TWAdjustmentsTextField.h"
@implementation TWAdjustmentsTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, self.leftInset, self.topInset);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, self.leftInset, self.topInset);
}
@end
leftInset
属性似乎工作正常,但topInset
属性似乎对UITextFields没有影响。还有其他方法可以调整UITextField
的顶部插图吗?
答案 0 :(得分:0)
也许,CGRectMake可以解决问题:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface TWAdjustmentsTextField : UITextField
@property (nonatomic) IBInspectable NSInteger topInset;
@property (nonatomic) IBInspectable NSInteger leftInset;
@end
#import "TWAdjustmentsTextField.h"
@implementation TWAdjustmentsTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + leftInset, bounds.origin.y + topInset, bounds.size.width - leftInset * 2, bounds.size.height - topInset * 2);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
@end
答案 1 :(得分:0)
我无法保证任何负面影响,但这似乎与我尝试做的事情有关。
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rect = bounds;
rect.origin.x += self.leftInset;
rect.origin.y += self.topInset;
return rect;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rect = bounds;
rect.origin.x += self.leftInset;
rect.origin.y += self.topInset;
return rect;
}
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
CGRect rect = bounds;
rect.origin.x += self.leftInset;
rect.origin.y += self.topInset;
return rect;
}