ios - 将类别添加到 UITextView 并在 UITextView 在 xib 时覆盖它的 init 函数

标签 ios objective-c uitextview xib objective-c-category

我无法拦截在 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/

相关文章:

ios - iOS 应用程序的用户界面似乎因未知原因而被放大

ios - WiFi打开时如何在iPhone中获取移动数据状态

ios - 仅当有小数部分时,CGFloat 到带小数的 NSString

ios - iOS 浏览器 (Chrome/Safari) 上的 Facebook Like 按钮和 Twitter Follow 按钮未内联

ios - NSTimer 有错误消息?

objective-c - OS X 文件系统 API 比重复调用 [NSFileManager attributeOfItemAtPath...] 更快?

javascript - react native (RCT_REMAP_METHOD): How to export a method with a parameter and return value?

ios - Swift 中 UITextView 和 UITextField 的单一扩展

swift - UITextview 的 contentInset 和 scrollIndicatorInsets 与 Swift 中的键盘和旋转?

ios - iOS模拟器中的UITextView输入光标问题