iOS textField,font设置和placeHolder,font设置

遇到一个bug,textField的font,月placeHolder的font分开设置遇到的问题.

先上代码:

定义了一个处理AttributedString的分类,主要是为了方便后续使用

@implementation NSString (AttributeString)

- (NSAttributedString *)getAttributeStringWithColor:(UIColor *)color fontSize:(CGFloat)fontSize {
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:self];
    [attriString addAttribute:NSForegroundColorAttributeName
                        value:color
                        range:NSMakeRange(0,[self length])];
    [attriString addAttribute:NSFontAttributeName
                        value:[UIFont systemFontOfSize:fontSize]
                        range:NSMakeRange(0,[self length])];
    return [attriString copy];
}

@end

接下来是使用过程:

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectZero];
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.textColor = UIColorFromRGB(0x222222);
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.font = [UIFont systemFontOfSize:16];
        _textField.adjustsFontSizeToFitWidth = YES;
        NSString *placeholder = @"输入商品代码或名称";
        _textField.attributedPlaceholder = [placeholder getAttributeStringWithColor:UIColorFromRGB(0x999999) fontSize:14];
        [_textField addTarget:self action:@selector(textFieldValueChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

问题来了,如果是在约束情况下布局,而且实际情况是 [textfield + 商品各种信息] 占据一整行,而且这个textfield的实际输入宽度还不到placeholder的宽度,这时候用约束或多或少会挤压一些控件,导致一些控件显示不太正常.

那么这个时候就需要针对textfield进行宽度处理,比如说在没有显示商品信息的时候,扩大textfield的宽度,在显示了商品信息的时候缩小textfield的宽度.主要是没有显示placeholder的时候textfield不需要那么宽

对textfield进行动态大小调整

- (void)textFieldValueChange:(id)sender {
    NSString *str = self.textField.text;
    if (str.length > 0) {
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_RATIO_CEIL(95));
        }];
    } else {
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_RATIO_CEIL(162));
        }];
    }
}

包括在清空数据的时候,也需要进行宽度约束控制

这时候问题出现了,因为控件有可能由小变大,导致placeholder的字体大小也出现了变化,变的跟之前textfield的font大小一样了都是16...

所以需要对placeholder的自提需要重新进行设置

    NSString *placeholder = @"输入商品代码或名称";
    self.textField.attributedPlaceholder = [placeholder getAttributeStringWithColor:UIColorFromRGB(0x999999) fontSize:FONT_RATIO(14)];

但是这么做还是不行.于是我添加上了[self layoutIfNeeded];进行重新绘制,也是不行

    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    NSString *placeholder = @"输入商品代码或名称";
    self.textField.attributedPlaceholder = [placeholder getAttributeStringWithColor:UIColorFromRGB(0x999999) fontSize:FONT_RATIO(14)];

最终先调用[self setNeedsLayout]; [self layoutIfNeeded]; 之后在进行placeholder大小设置才成功.

具体原因应该需要在textfield大小固定后在设置placeholder的大小才有效果.

具体可以了解setNeedsLayout与layoutIfNeeded
参考链接:https://www.jianshu.com/p/d46bcc656e04

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容