如何在 iPhone UILabel 中设置字距调整
我正在开发一个 iPhone 应用程序,我想在 UILabel 中设置字距调整。我编写的代码(可能在 kCTKernAttributeName
周围)似乎有错误。我该如何解决这个问题?
NSMutableAttributedString *attStr;
NSString *str = @"aaaaaaa";
CFStringRef kern = kCTKernAttributeName;
NSNumber *num = [NSNumber numberWithFloat: 2.0f];
NSDictionary *attributesDict = [NSDictionary dictionaryWithObject:num
forKey:(NSString*)kern];
[attStr initWithString:str attributes:attributesDict];
CGRect frame1 = CGRectMake(0, 0, 100, 40);
UILabel *label1 = [[UILabel alloc] initWithFrame:frame1];
label1.text = attStr
[self.view addSubview:label1];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
老问题,但你现在可以(很容易)做到。
2013 年 11 月,为了扩展这个很好的答案,这里有一些完全典型的代码。通常你也会设置字体。请注意评论中使用普通旧 .text 的老式方式。希望它可以帮助某人
Old question, but you can do it now (easily).
For Nov 2013, Just to expand on this great answer, here's some totally typical code. Usually you'd set the font as well. Note in the comments the old-fashioned way using ordinary old .text. Hope it helps someone
之前:
之后:
这是一个 Swift 3 扩展,可让您通过代码或故事板设置 UILabel 的字距调整:
演示用法:
或
该演示使用 3.0 字距调整来表现戏剧性,但我发现 0.1 - 0.8 在实践中往往效果很好。
Before:
After:
Here's a Swift 3 extension that let's you set a UILabel's kerning via code or storyboard:
Demo usage:
or
The demo uses 3.0 kerning for drama, but I've found 0.1 - 0.8 tends to work well in practice.
根据 DBD 的回答,我在 UILabel 上创建了一个类别,如果在 iOS6+ 上运行,则允许设置字距调整,并优雅地回退到仅在以前的 iOS 版本上设置文本。可能对其他人有帮助...
UILabel+TextKerning.h
UILabel+TextKerning.m
Taking DBD's answer, I made a category on UILabel which allows setting the kerning if running on iOS6+ with graceful fall back to just setting text on previous iOS versions. Might be of help to others...
UILabel+TextKerning.h
UILabel+TextKerning.m
为了保持最新状态,iOS 6 为
UILabel
和UITextView
引入了 attributeText!UILabel参考:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/attributedText
Just to be up-to-date here, iOS 6 introduced attributedText for
UILabel
andUITextView
!UILabel reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/attributedText
只需在 Swift 中执行此操作即可:
Just do this in Swift:
使用 IBDesignables 和 IBInspectables 的示例,您可以仅通过故事板设置字距调整值。
我发现它非常实用,我想与你分享。
UILabelKerning.h
UILabelKerning.m
An example using IBDesignables and IBInspectables where you can manage to set the kerning value through storyboard only.
I found it very practical and I thought to share it with you.
UILabelKerning.h
UILabelKerning.m
据我所知,
UILabel
不会呈现NSAttributedString
的特征。有一些不错的开源解决方案。我最近使用 TTTAttributedLabel 作为接受 NSAttributedString 的 UILabel 的替代品。DTCoreText(以前的 NSAttributedString+HTML)最近也引起了一些关注。
As far as I am aware,
UILabel
will not render the characteristics ofNSAttributedString
. There are a couple of nice open source solutions. I recently used TTTAttributedLabel as a swap in replacement for UILabel that accepts NSAttributedString.DTCoreText (former NSAttributedString+HTML) is also getting a bit of buzz lately.
Swift 4 和 5
Swift 4 and 5
在 Swift 2.0 中...
添加扩展:
现在,只需将 UILabel 设置为 attributeText:
显然,我添加了一堆您可能不需要的参数。尝试一下——随意重写这个方法——我在一堆不同的答案中寻找这个,所以我想我会发布整个扩展,以防它对那里的人有帮助......-rab
In Swift 2.0...
Add an extension:
Now, just set your UILabel as attributedText:
Obviously, I added a bunch of parameters that you may not need. Play around -- feel free to rewrite the method -- I was looking for this on a bunch of different answers so figured I'd post the whole extension in case it helps someone out there... -rab