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