Drop Cap with Nsattributedstring

Drop cap with NSAttributedString

CoreText cannot do drop caps because it consists of lines made up of glyph runs. A drop cap would cover multiple lines which is not supported.

To achieve this effect you would have to draw the cap separately and then draw the rest of the text in a path that goes around it.

Long story short: not possible in UILabel, possible, but a fair bit of work with CoreText.

The steps to do it with CoreText are:

  • create a framesetter for the single character.
  • get its bounds
  • create a path that spares out the frame of the drop cap
  • create a framesetter for the remaining characters with this path
  • draw first glyph
  • draw rest

Indenting UIlabel around another UILabel (Drop Capping)

What you seem to be looking for is CoreText. See the following answer:

Drop cap with NSAttributedString

Core Text: Drop caps + paragraph styles: Incompatible?

You can use straight Core Text to achieve this, in the following post I explain the use of 2 framesetters to lay out text with drop caps in a UIView. In the code example (there's also a link to a github repo) you'll be able to see where the paragraph styles are created and applied to the main text view.

https://stackoverflow.com/a/14639864/1218605

HTML CSS How to drop caps words instead of just first letter?

Here is a structure you can use, with Flexbox

.inner {  display: flex;  align-items: flex-end;}.inner > div:nth-child(1) {  font-size: 60px;  text-transform: uppercase;}.inner2 {  padding: 0 0 15px 10px;}.inner2 div:nth-child(-n+2) {  display: inline-block;}.inner2 > div:nth-child(1) {  font-size: 20px;  text-transform: uppercase;}.wrapper > div:nth-child(2) {  font-size: 28px;  text-transform: uppercase;}.wrapper > div:nth-child(3) {  font-size: 50px;  text-transform: uppercase;}
<div class="wrapper">  <div class="inner">    <div>Strength</div>    <div class="inner2">      <div>is not</div>      <div>something</div>      <div>you have, rather</div>    </div>  </div>  <div>something that reveals itself</div>  <div>when you need it</div></div>

Core Text - NSAttributedString line height done right?

I'm still not 100% confident in my following statements, but it seems to make sense. Please correct me where I am wrong.

The line height (leading) refers to the distance between the baselines of successive lines of type. The baseline here can be interpreted as the imaginary line which the text sits on.

Spacing is the space between lines. The space appears after the line of text.

I ended up using the following solution to my problem:

// NOT SURE WHAT THE THEORY BEHIND THIS FACTOR IS. WAS FOUND VIA TRIAL AND ERROR.
CGFloat factor = 14.5/30.5;
CGFloat floatValues[4];
floatValues[0] = self.lineHeight * factor/(factor + 1);
floatValues[1] = self.lineHeight/(factor + 1);
floatValues[2] = self.lineHeight;

This matrix is used with the paragraph style parameter for NSAttributedString:

CTParagraphStyleSetting paragraphStyle[3];

paragraphStyle[0].spec = kCTParagraphStyleSpecifierLineSpacing;
paragraphStyle[0].valueSize = sizeof(CGFloat);
paragraphStyle[0].value = &floatValues[0];

paragraphStyle[1].spec = kCTParagraphStyleSpecifierMinimumLineHeight;
paragraphStyle[1].valueSize = sizeof(CGFloat);
paragraphStyle[1].value = &floatValues[1];

paragraphStyle[2].spec = kCTParagraphStyleSpecifierMaximumLineHeight;
paragraphStyle[2].valueSize = sizeof(CGFloat);
paragraphStyle[2].value = &floatValues[2];

CTParagraphStyleRef style = CTParagraphStyleCreate((const CTParagraphStyleSetting*) ¶graphStyle, 3);
[attributedString addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0, [string length])];
CFRelease(style);

Hope this helps someone. I'll update this answer as I discover more relevant information.



Related Topics



Leave a reply



Submit