Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

ios7 - Adjust letter spacing in iOS 7

In iOS 7, when navigating back using the new swipe-from-edge-of-screen gesture, the title of the Back button ("Artists") fades from being pink (in the example below) and having regular font weight to being black and having bold font weight.

enter image description here

It seems to me that the animation uses two different labels in order to achieve this effect; one fading out as the other fades in. However, Apple has somehow adjusted the font so that the regular label perfectly overlays the bold one, thus creating the illusion of a single label morphing between two different weights and colors.

Have they simply adjusted the letter spacing on the regular font so that it matches onto the bold one? In that case, how would that be achieved in iOS 7? Does Text Kit have any awesome features for doing this or how should I go about it?

question from:https://stackoverflow.com/questions/21140683/adjust-letter-spacing-in-ios-7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can adjust letter spacing like this, using NSAttributedString.

In Objective-C:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"The Clash"];
[attributedString addAttribute:NSKernAttributeName
                         value:@(1.4)
                         range:NSMakeRange(0, 9)];
    
self.label.attributedText = attributedString;

In Swift 3:

let attributedString = NSMutableAttributedString(string: "The Clash")
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.4), range: NSRange(location: 0, length: 9))

label.attributedText = attributedString

In Swift 4 and later:

label.attributedText = NSAttributedString(string: "The Clash", attributes: [.kern: 1.4])

More info on kerning is available in Typographical Concepts from the Text Programming Guide.

I don't think there's a TextKit feature that will automatically match font spacing between bold and regular text.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...