【问题标题】:Changing UITextField top inset not working更改 UITextField 顶部插图不起作用
【发布时间】:2016-01-06 06:58:25
【问题描述】:

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

【问题讨论】:

  • 我想知道你是否应该调整字体大小。
  • @Lumialxk 字体大小对我的问题没有帮助。我的问题是我需要偏移 UITextField 内的文本,以便它稍微对齐 UITextField 的底部边缘

标签: ios objective-c iphone uitextfield


【解决方案1】:

也许,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

【讨论】:

    【解决方案2】:

    我不能保证任何负面影响,但这似乎与我正在尝试做的事情有关。

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2013-01-14
      • 2011-05-28
      相关资源
      最近更新 更多