Memory leaks &引用;“调整以适应”;导致UITextField类别(或子类)的内存泄漏
在UITextField上使用类别时,如果字段中的文本超出其可见边界,“调整到适合”和“最小字体大小”选项可能会导致内存泄漏。我尝试了子类化,但没有解决问题 下面是我的类别实现的样子:Memory leaks &引用;“调整以适应”;导致UITextField类别(或子类)的内存泄漏,memory-leaks,uitextfield,subclass,categories,Memory Leaks,Uitextfield,Subclass,Categories,在UITextField上使用类别时,如果字段中的文本超出其可见边界,“调整到适合”和“最小字体大小”选项可能会导致内存泄漏。我尝试了子类化,但没有解决问题 下面是我的类别实现的样子: @implementation UITextField (custom) static NSString *fontName = @"My-Awsome-Font"; static UIColor *color; static UIFont *smallFont; sta
@implementation UITextField (custom)
static NSString *fontName = @"My-Awsome-Font";
static UIColor *color;
static UIFont *smallFont;
static UIFont *largeFont;
static NSDictionary *smallAttributes;
static NSDictionary *largeAttributes;
static NSString *placeholder;
static bool shouldModifyPlaceholder;
- (CGRect)textRectForBounds:(CGRect)bounds
{
if(largeFont == nil)
largeFont = [UIFont fontWithName:fontName size:20.0];
if(smallFont == nil)
smallFont = [UIFont fontWithName:fontName size:16.0];
if(smallAttributes == nil)
smallAttributes = @{NSFontAttributeName: smallFont};
if(largeAttributes == nil)
largeAttributes = @{NSFontAttributeName: largeFont};
// general elements
[self setBackgroundColor:[UIColor whiteColor]];
[self.layer setBorderColor:[UIColor whiteColor].CGColor];
[self.layer setBorderWidth:2.0];
self.font = largeFont;
self.layer.cornerRadius = 3;
self.clipsToBounds = YES;
//
shouldModifyPlaceholder = [self respondsToSelector:@selector(setAttributedPlaceholder:)] && ![placeholder isEqualToString:self.placeholder];
// custom elements for each size category
if (self.frame.size.width <= 90) {
if (shouldModifyPlaceholder) {
if ([self.placeholder length] > 3) {
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:smallAttributes];
}
else {
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:largeAttributes];
}
placeholder = self.placeholder;
}
return CGRectMake(bounds.origin.x + 18, bounds.origin.y + 9,
bounds.size.width - 36, bounds.size.height - 16);
}
else {
if (shouldModifyPlaceholder) {
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:largeAttributes];
placeholder = self.placeholder;
}
return CGRectMake(bounds.origin.x + 20, bounds.origin.y + 9,
bounds.size.width - 40, bounds.size.height - 16);
}
}
@实现UITextField(自定义)
静态NSString*fontName=@“我的一些字体”;
静态UIColor*颜色;
静态UIFont*小字体;
静态UIFont*大字体;
静态NSDictionary*小属性;
静态NSDictionary*大型属性;
静态NSString*占位符;
静态布尔值应修改占位符;
-(CGRect)textRectForBounds:(CGRect)bounds
{
如果(大字体==nil)
largeFont=[UIFont fontWithName:fontName大小:20.0];
如果(smallFont==nil)
smallFont=[UIFont fontWithName:fontName大小:16.0];
if(smallAttributes==nil)
smallAttributes=@{NSFontAttributeName:smallFont};
如果(大属性==nil)
largeAttributes=@{NSFontAttributeName:largeFont};
//一般要素
[self-setBackgroundColor:[UIColor whiteColor]];
[self.layer setBorderColor:[UIColor whiteColor].CGColor];
[self.layer-width:2.0];
self.font=大字体;
self.layer.cornerRadius=3;
self.clipstobunds=是;
//
shouldModifyPlaceholder=[self respondsToSelector:@selector(setAttributedPlaceholder:)]和&![placeholder IsequalString:self.placeholder];
//每个尺寸类别的自定义元素
if(自框尺寸宽度3){
self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:self.placeholder属性:smallAttributes];
}
否则{
self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:self.placeholder属性:LargeAttribute];
}
占位符=self.placeholder;
}
返回CGRectMake(bounds.origin.x+18,bounds.origin.y+9,
bounds.size.width-36,bounds.size.height-16);
}
否则{
如果(应修改占位符){
self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:self.placeholder属性:LargeAttribute];
占位符=self.placeholder;
}
返回CGRectMake(bounds.origin.x+20,bounds.origin.y+9,
bounds.size.width-40,bounds.size.height-16);
}
}
最近,我发现在UITextField中键入“太多”文本会导致我的应用程序耗尽所有手机内存并崩溃。我发现这是因为我的定制与“调整以适应”选项不匹配。删除“调整以适应”修复了该问题。我还将“最小字体大小”属性设置为原始字体大小以确保。由于我花了一天多的时间试图找到一个解决方案,我想我会分享这个,以防其他人遇到这个问题
最近,我发现在UITextField中键入“太多”文本会导致我的应用程序耗尽所有手机内存并崩溃。我发现这是因为我的定制与“调整以适应”选项不匹配。删除“调整以适应”修复了该问题。我还将“最小字体大小”属性设置为原始字体大小以确保。由于我花了一天多的时间试图找到一个解决方案,我想我会分享这个,以防其他人遇到这个问题