ios - 来自NSAttributedString的UITextField下划线只有1个像素高吗?

标签 ios uitextfield nsattributedstring underline

标签(图片的底部)和文本字段(顶部)具有相同的attributedText。但是请看下划线。文本字段中的像素只有一个像素高。这看起来很糟糕。有谁知道是什么原因或如何预防的?

- (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提交错误。谢谢!

关于ios - 来自NSAttributedString的UITextField下划线只有1个像素高吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617135/

相关文章:

ios - 如何为时间间隔创建属性字符串,如 iOS 10 上的事件应用程序中所见的 3 小时 32 分钟?

objective-c - 带有 attributeTitle 的 NSMenuItem 在 macOS 10.12.2 Sierra 中不起作用

ios - 是否可以使用 eventkit 更改 icalendar 中的默认警报时间?

ios - iPhone登录UINavigationController内的其他页面

ios - iPhone : How can I have my refresh UIBarButtonItem change to UIActivityIndicator while task is running?

ios - UITextField:如何对 "Done"键使用react并验证输入

ios - 类型 'Model' 不符合协议(protocol) 'Decodable'/Encodable

ios - Swift - 如何强制用户在 UITextField 中只导入整数?

ios - 以编程方式实现 UITextField 标准样式

swift - 如何仅更改字符串中最后两个字符的字体