更改UITextField顶部插入不起作用

时间:2016-01-06 06:58:25

标签: ios objective-c iphone uitextfield

我将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的顶部插图吗?

2 个答案:

答案 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;
}