如何在UILabel中添加下划线的文本?

编程入门 行业动态 更新时间:2024-10-23 15:32:12
本文介绍了如何在UILabel中添加下划线的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 UILabel 中加上下划线的文字?

$ c> UILabel ,高度 0.5f ,并将其放在文本下。当我在 iPhone 4.3 和 iPhone 4.3模拟器中运行我的应用时,此标签不可见,但在 iPhone 5.0模拟器 。

解决方案

目标 - 为什么?

C

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

Swift

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

iOS 5.1.1<版本

您需要 3方归属标签才能显示归属文字:

1)请参阅 TTTAttributedLabel 链接。其最佳第三方归属 标签到显示归属 text 。

2)请参阅 OHAttributedLabel 为第三方归属 标签

How can I make an underlined text in UILabel?

I had tried by making a UILabel with height 0.5f and place it under the text. This line label is not visible when I am running my app in iPhone 4.3 and iPhone 4.3 simulator, but it is visible in iPhone 5.0 simulator onwards. Why?

How can I do this?

解决方案

Objective-C

iOS 6.0 > version

UILabel supports NSAttributedString

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello Good Morning"]; [attributeString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:(NSRange){0,[attributeString length]}];

Swift

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Hello Good Morning") attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))

Definition :

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange

Parameters List:

name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

value : The attribute value associated with name.

aRange : The range of characters to which the specified attribute/value pair applies.

Now use like this:

yourLabel.attributedText = [attributeString copy];

iOS 5.1.1 < version

You needs 3 party attributed Label to display attributed text:

1) Refer TTTAttributedLabel link. Its best third party attributed Label to display attributed text.

2) refer OHAttributedLabel for third party attributed Label

更多推荐

如何在UILabel中添加下划线的文本?

本文发布于:2023-06-07 21:57:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下划线   文本   如何在   UILabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!