UITextField underlines from NSAttributedString are only 1 pixel high?

青春壹個敷衍的年華 提交于 2019-12-06 12:37:26

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/13617135/uitextfield-underlines-from-nsattributedstring-are-only-1-pixel-high

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!