Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to adjust the baseline in NSAttributedString?

I'd like to slightly modify the y position of a character in an NSAttributedString. I know I can subscript (or superscript) using the attribute dictionary:

@{(id)kCTSuperscriptAttributeName: @(-1)}

Unfortunately the shift caused by this is too large in my case. I'm looking for an option to adjust the baseline in 1-point steps.

The attributed string will be displayed in a UIButton using -setAttributedTitle:forState:.

like image 366
Ortwin Gentz Avatar asked Jan 11 '13 15:01

Ortwin Gentz


2 Answers

As of iOS 7 the NSBaselineOffsetAttributeName attribute has been added.

like image 129
Tommy Avatar answered Oct 15 '22 21:10

Tommy


Yes you can... but only if you draw the text yourself like I am doing it in my DTCoreText open source project.

See line 924ff in DTCoreTextLayoutFrame.m here: https://github.com/Cocoanetics/DTCoreText/blob/master/Core/Source/DTCoreTextLayoutFrame.m

Unfortunately that's the ONLY way I know how to. UIKit does not give you this option.

like image 38
Cocoanetics Avatar answered Oct 15 '22 23:10

Cocoanetics