我无法拦截在 xib 文件中创建时调用的 init 函数。
我想在创建它时为其添加边界线,这样我就不需要手动添加它。
。H:
#import <UIKit/UIKit.h>
@interface UITextView (FITAddBorderline)
- (id) init;
- (id) initWithFrame:(CGRect)frame;
@end
米:
#import "UITextView+FITAddBorderline.h"
@implementation UITextView (FITAddBorderline)
- (id) init
{
self = [super init];
if (self) {
[self addBorderline];
}
return self;
}
- (id) initWithFrame:(CGRect)frame {
self = [super init];
if (self) {
self.frame = frame;
[self addBorderline];
}
return self;
}
- (void) addBorderline {
//To make the border look very close to a UITextField
[self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[self.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
@end
最佳答案
来自 NIB 的 View 初始化为 initWithCoder:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self addBorderline];
}
return self;
}
作为旁注,我建议更改您正在做的事情并使用子类而不是类别。您可能会遇到一些麻烦,覆盖类别中的方法。 See more info here.
关于ios - 将类别添加到 UITextView 并在 UITextView 在 xib 时覆盖它的 init 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24030574/