如何在UILabel
中制作下划线文本?
我尝试了通过创建高度为0.5f的UILabel
并将其放置在文本下方来实现。但是当我在iPhone 4.3和iPhone 4.3模拟器上运行应用程序时,这条线标签是不可见的,但在iPhone 5.0模拟器及以上版本中可见。为什么会这样呢?
如何实现这个效果呢?
如何在UILabel
中制作下划线文本?
我尝试了通过创建高度为0.5f的UILabel
并将其放置在文本下方来实现。但是当我在iPhone 4.3和iPhone 4.3模拟器上运行应用程序时,这条线标签是不可见的,但在iPhone 5.0模拟器及以上版本中可见。为什么会这样呢?
如何实现这个效果呢?
iOS 6.0及以上版本可用
UILabel
支持NSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello Good Morning"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))
定义:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
参数列表:
name:一个字符串,指定属性名称。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关在NSAttributedString类参考中查找系统提供的属性键的位置信息,请参见概述部分。
value:与名称相关联的属性值。
aRange:指定属性/值对适用于哪些字符范围。
现在可以按以下方式使用:
yourLabel.attributedText = [attributeString copy];
你需要使用第三方的带属性标签来显示带属性文本:
1) 可以参考TTTAttributedLabel链接。这是最好的第三方标签,用于显示带属性文本。
2) 可以参考OHAttributedLabel,它也是第三方的带属性标签。
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Hello Good Morning")
中,attributeString
的数据类型已经被赋值的对象所隐含,因此它是多余的,你可以省略它,如下所示:let attributeString = NSMutableAttributedString(string: "Hello Good Morning")
。 - clearlight我正在使用 Xcode 9 和 iOS 11。 想要在 UILabel 下方添加下划线。 你可以使用以下两种方法: 1. 使用代码 2. 使用 xib
使用代码:
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"I am iOS Developer"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
lblAttributed.attributedText = attributeString;
使用Xib:
我个人喜欢尽可能地自定义我的设计。有时候,你会发现使用内置的下划线工具不容易进行自定义。
以下是我如何做到的:
1首先创建一个UIView用于下划线。您可以在IB中或通过编程方式选择背景颜色。这里关键在于约束。
2然后,您可以根据需要自定义下划线。例如,为了适应更好的视觉效果而调整下划线的宽度,请在Interface Builder中创建一个outlet connection到一个constraint。
3然后,您可以轻松地通过编程方式自定义您的下划线视图。例如,在这里,为了给您一个想法,我们将使下划线比标题“Breads”宽20像素:
var originalString: String = "Breads"
let myString: NSString = originalString as NSString
let size: CGSize = myString.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])
self.widthUnderlineTitle.constant = size.width + 20
结果:
请看 TTTAttributedLabel。如果你可以使用第三方组件,只需将你的UILabel替换为TTTAttributedLabel,并在需要时使用它(完美替代)。支持iOS < 6.0!
UILabel的Swift扩展:
仍需要改进,欢迎任何想法:
extension UILabel{
func underLine(){
if let textUnwrapped = self.text{
let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let underlineAttributedString = NSAttributedString(string: textUnwrapped, attributes: underlineAttribute)
self.attributedText = underlineAttributedString
}
}
}
目前我能想到的缺点是,每次文本更改时都需要调用它,而实际上没有关闭它的方法...
Swift 4.0
var attributeString = NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSRange)
Swift 5
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: yourLabel.text ?? "")
attributeString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel.attributedText = attributeString