UIlabel 中的文本下划线
如何为可能是多行字符串的文本添加下划线? 我发现有些人建议 UIWebView,但对于仅文本渲染来说,它显然是一个太重的类。
我的想法是找出每行中每根弦的起点和长度。 并在其下方相应地画一条线。
我遇到了如何计算字符串的长度和起点的问题。
我尝试使用 -[UILabel textRectForBounds:limitedToNumberOfLines:]
,这应该是文本的绘图边界矩形,对吧? 那我还得做对齐工作吗? 当每条线居中对齐和右对齐时,如何获得它的起点?
How can I underline a text that could be multiple lines of string?
I find some people suggest UIWebView, but it is obviously too heavy a class for just text rendering.
My thoughts was to figure out the start point and length of each string in each line.
And draw a line under it accordingly.
I meet problems at how to figure out the length and start point for the string.
I tried to use -[UILabel textRectForBounds:limitedToNumberOfLines:]
, this should be the drawing bounding rect for the text right?
Then I have to work on the alignment?
How can I get the start point of each line when it is center-justified and right justified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
您可以从 UILabel 子类化并重写 drawRect 方法:
UPD:
从 iOS 6 开始,Apple 添加了对 UILabel 的 NSAttributedString 支持,因此现在它更容易并且适用于多行:
如果您仍然希望支持 iOS 4 和 iOS 5,我建议使用 TTTAttributedLabel 而不是手动为标签添加下划线。但是,如果您需要为单行 UILabel 添加下划线并且不想使用第三方组件,则上面的代码仍然可以解决问题。
You may subclass from UILabel and override drawRect method:
UPD:
As of iOS 6 Apple added NSAttributedString support for UILabel, so now it's much easier and works for multiple lines:
If you still wish to support iOS 4 and iOS 5, I'd recommend to use TTTAttributedLabel rather than underline label manually. However if you need to underline one-line UILabel and don't want to use third-party components, code above would still do the trick.
在斯威夫特中:
In Swift:
这就是我所做的。它的作用就像黄油一样。
1) 将 CoreText.framework 添加到您的框架中。
2)导入;在需要下划线标签的类中。
3) 编写以下代码。
This is what i did. It works like butter.
1) Add CoreText.framework to your Frameworks.
2) import <CoreText/CoreText.h> in the class where you need underlined label.
3) Write the following code.
使用属性字符串:
然后覆盖标签 - (void)drawTextInRect:(CGRect)aRect 并以类似以下方式呈现文本:
或者更好的是,不要覆盖,只需使用 OHAttributedLabel 由 Olivier Halligon 创建
Use an attribute string:
And then override the label - (void)drawTextInRect:(CGRect)aRect and render the text in something like:
Or better yet instead of overriding just use the OHAttributedLabel created by Olivier Halligon
我结合了一些提供的答案,以创建更好的(至少满足我的要求) UILabel 子类,它支持:
https://github.com/GuntisTreulands/UnderLineLabel
I've combined some of provided answers, to create better (at least for my requirements) UILabel subclass, which supports:
https://github.com/GuntisTreulands/UnderLineLabel
不想子类化视图(UILabel/UIButton)等的人......
'forgetButton' 也可以被任何标签替换。
People, who do not want to subclass the view (UILabel/UIButton) etc...
'forgetButton' can be replace by any lable too.
另一个解决方案可能是(自 iOS 7 起)为
NSBaselineOffsetAttributeName
赋予负值,例如您的NSAttributedString
可能是:希望这会有所帮助;-)
Another solution could be (since iOS 7) given a negative value to
NSBaselineOffsetAttributeName
, for example yourNSAttributedString
could be:Hope this will help ;-)
您可以创建一个名为 UnderlinedLabel 的自定义标签并编辑 drawRect 函数。
You can create a custom label with name UnderlinedLabel and edit drawRect function.
这是最适合我的解决方案,无需编写额外的代码。
Here is the easiest solution which works for me without writing additional codes.
有时我们开发人员会陷入任何 UI 屏幕的小设计部分。最令人恼火的要求之一是在行文本下。别担心,这里有解决方案。
使用 Objective C 在 UILabel 中的文本下划线
使用 Swift 在 UILabel 中的文本下划线
Sometimes we developer stuck in small designing part of any UI screen. One of the most irritating requirement is under line text. Don’t worry here is the solution.
Underlining a text in a UILabel using Objective C
Underlining a text in UILabel using Swift
Kovpas代码的增强版(颜色和线条尺寸)
An enhanced version of the code of Kovpas (color and line size)
我已经为带下划线的多行 uilabel 创建了:
对于字体大小 8 到 13 set int lineHeight = self.font.pointSize+3;
对于字体大小 14 到 20,设置 int lineHeight = self.font.pointSize+4;
I have Created for multiline uilabel with underline :
For Font size 8 to 13 set int lineHeight = self.font.pointSize+3;
For font size 14 to 20 set int lineHeight = self.font.pointSize+4;
斯威夫特 4.1 版本:
Swift 4.1 ver:
正如 kovpas 所示,在大多数情况下您可以使用边界框,尽管并不总是保证边界框能够整齐地贴合文本。根据 UILabel 配置,高度为 50、字体大小为 12 的框可能无法给出您想要的结果。
使用 kovpas 已提供的绘图代码查询 UILabel 中的 UIString 以确定其确切指标,并使用这些指标更好地放置下划线,而不管封闭的边界框或框架如何。
您还应该查看 UIFont 的“leading”属性,该属性根据特定字体给出基线之间的距离。基线是您希望绘制下划线的位置。
查找 UIKit 对 NSString 的添加:
As kovpas has shown you can use the bounding box in most cases, although it is not always guaranteed that the bounding box will fit neatly around the text. A box with a height of 50 and font size of 12 may not give the results you want depending on the UILabel configuration.
Query the UIString within the UILabel to determine its exact metrics and use these to better place your underline regardless of the enclosing bounding box or frame using the drawing code already provided by kovpas.
You should also look at UIFont's "leading" property that gives the distance between baselines based on a particular font. The baseline is where you would want your underline to be drawn.
Look up the UIKit additions to NSString:
我使用开源线视图,并将其添加到按钮子视图中:
这是受到上面 Karim 的启发。
I use an open source line view and just added it to the button subviews:
This was inspired by Karim above.
基于 Kovpas 和Damien Praca 的答案,这里是 UILabelUnderligned 的实现,它也支持 textAlignemnt。
和实施:
Based on Kovpas & Damien Praca's Answers, here is an implementation of UILabelUnderligned which also support textAlignemnt.
and the implementation:
这是另一个更简单的解决方案(下划线的宽度不是最准确的,但对我来说已经足够好了)
我有一个 UIView
(_view_underline)
,它有白色背景,高度为 1 像素,每次更新它的宽度更新文本Here's another, simpler solution (underline's width is not most accurate but it was good enough for me)
I have a UIView
(_view_underline)
that has White background, height of 1 pixel and I update its width everytime I update the textNSUnderlineStyleAttributeName 采用 NSNumber(其中 0 没有下划线)可以添加到属性字典中。
我不知道这是否更容易。但是,这对我的目的来说更容易。
NSUnderlineStyleAttributeName which takes an NSNumber (where 0 is no underline) can be added to an attribute dictionary.
I don't know if this is any easier. But, it was easier for my purposes.
您可以使用我的自定义标签!
您还可以使用界面生成器来设置
You can use this my custom label!
You can also use interface builder to set