来自NSAttributedString的UITextField下划线只有1个像素高? [英] UITextField underlines from NSAttributedString are only 1 pixel high?

查看:121
本文介绍了来自NSAttributedString的UITextField下划线只有1个像素高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标签(图片底部)和文本字段(顶部)具有相同的attributionText。但请看下划线。文本字段中的那些只有一个像素高。这看起来很可怕。有谁知道造成这种情况的原因或如何防止它?

The label (bottom of the picture) and the text field (top) have the same same attributedText. But look at the underlines. The ones in the text field are only one pixel high. This looks terrible. Does anyone know what is causing this or how to prevent it?

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 600, 200)];
        NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:@"The quick brown fox jumps"];
        NSNumber* underlineNumber = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
        UIFont* font = [UIFont systemFontOfSize: 50];
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
        for (NSInteger i=0; i<20; i++) {
            if (i%3==0) {
                [string addAttribute:NSUnderlineStyleAttributeName value:underlineNumber range:NSMakeRange(i, 1)];
            }
        }
        textField.backgroundColor = [UIColor whiteColor];
        textField.attributedText = string;
        [self addSubview:textField];
        UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(100, 400, 600, 200)];
        label.attributedText = string;
        label.font = font;
        label.backgroundColor = [UIColor whiteColor];
        [self addSubview:label];
    }
    return self;
}

推荐答案

标签使用自定义渲染样式绘制下划线遗憾的是,它不同于UITextField使用的那个,它在编辑时使用WebKit进行渲染,而在静态时使用Core Text进行渲染。请提交bugreporter.apple.com的错误。谢谢!

The label uses a custom rendering style to draw the underline which is unfortunately distinct from the one used by UITextField, which uses WebKit to render when editing and Core Text to render when it's static. Please file a bug with bugreporter.apple.com. Thanks!

这篇关于来自NSAttributedString的UITextField下划线只有1个像素高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆