ios uitextview 下划线距离文字
在iOS的UITextView中,可以使用NSAttributedString来设置下划线和调整下划线与文字之间的距离。
首先,你需要创建一个NSMutableAttributedString对象,并设置其属性。在这个对象中,你可以设置下划线的样式、颜色和粗细。
接下来,你可以通过使用NSMutableParagraphStyle来设置下划线与文字之间的距离。使用paragraphStyle的underlineOffset属性可以调整下划线的位置。
下面是一个示例代码:
// 创建NSMutableAttributedString对象
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Hello World!"];
// 设置下划线样式
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attributedString.length)];
// 设置下划线颜色
[attributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedString.length)];
// 设置下划线粗细
[attributedString addAttribute:NSStrokeWidthAttributeName value:@(1.0) range:NSMakeRange(0, attributedString.length)];
// 创建NSMutableParagraphStyle对象
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 设置下划线位置
[paragraphStyle setUnderlineOffset:5.0];
// 将paragraphStyle应用到attributedString
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
// 将attributedString应用到UITextView
textView.attributedText = attributedString;
上述代码会在UITextView中显示带有下划线的文本,并且下划线与文字之间有5个点的距离。你可以根据需要调整underlineOffset的值来改变下划线的位置
原文地址: http://www.cveoy.top/t/topic/h1nR 著作权归作者所有。请勿转载和采集!