2017-03-01 43 views
0

当使用上的UITextField类别,“调整以适应”,而当这样的字段内的文本超过最小字体大小选项可能会导致内存泄漏,其可见的边界。我尝试了子类化,但它没有解决问题。“调整,以适应”导致内存泄漏为UITextField类别(或子类别)

这里是我的类实现看起来像:

@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); 
     } 
    } 

回答

0

最近我发现,键入“太多”文成的UITextField引起我的应用程序使用了所有的手机的内存和崩溃。我发现这是由于我的自定义不适合“调整为适合”选项。删除“调整为适合”解决了问题。我还将最小字体大小属性设置为原始字体大小。因为我花了一天的时间试图寻找解决方案,所以我想我会分享这个以防其他人遇到这个问题。

change these properties