22
Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Advance text rendering in i os

  • Upload
    confiz

  • View
    1.337

  • Download
    7

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Advance text rendering in i os

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Page 2: Advance text rendering in i os

Advance Text Rendering In iOS Sohail Mohabbat Ali| iOS Mentor

Page 3: Advance text rendering in i os

Advance Text Rendering In iOS Topics covered in the presentation

• Back Ground

• Core Text

• How to Do It?

• How to Fix IT

or Sohail Mohabbat Ali | iOS Mentor

Page 4: Advance text rendering in i os

Advance text Rendering in iOS

Text Rendering in iOS

Sohail Mohabbat Ali | iOS Mentor

• UIKit

• Core Graphics/Quartz

• Core Text

Page 5: Advance text rendering in i os

Advance text Rendering in iOS

Text Rendering in iOS Cont..

Sohail Mohabbat Ali | iOS Mentor

UIKit:

In UIKit you have and you add a word or a text line on

the screen by simple Drag-and-Drop in IB, but you cannot do

many things like strikethrough and custom formatting.

Page 6: Advance text rendering in i os

Advance text Rendering in iOS

Text Rendering in iOS Cont..

Sohail Mohabbat Ali | iOS Mentor

Core Graphics/Quartz:

In you can do pretty much everything the

system is capable of, but you need to calculate the position of each

glyph in your text and draw it on the screen.

Page 7: Advance text rendering in i os

Advance text Rendering in iOS

Text Rendering in iOS Cont..

Sohail Mohabbat Ali | iOS Mentor

Core Text:

lies right between the two! You have complete control over

position, layout, attributes like color and size, but Core Text takes care

of everything else for you – from word wrap to font rendering and more.

Page 8: Advance text rendering in i os

Advance text Rendering in iOS

Back Ground

Sohail Mohabbat Ali | iOS Mentor

A tale of two Apples

• The original Apple development team created “Classic” APIs

and

• The NeXT team created NS* APIs which we later called

“ ”

• Since the merge of the two company, solutions like

and were created to close the gap

• For modern, Unicode-aware text layout, there is which

is still a popular choice for now

Page 9: Advance text rendering in i os

Advance text Rendering in iOS

Background

Sohail Mohabbat Ali | iOS Mentor

More about ATSUI

• Short for “ ”

Like API (OSErr, Fixed, Pascal String, etc.)

• Fast, robust, powerful, but the API is bit complicate to use

(for font enumeration and selection) still exists, but ATSUI

is now , no 64-bit support

Page 10: Advance text rendering in i os

Advance text Rendering in iOS

Why Core Text ?

Sohail Mohabbat Ali | iOS Mentor

• Simpler, Core Foundation style API, much easier to call from

Cocoa apps

• Minimize between different data formats, improve

performance (up to 2 times faster according to Apple)

• Supported behavior (like font traits) closely matches with

Cocoa Text System

Page 11: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

• Lay formatted text down on the screen

• Fine tune the text’s appearance

• Add images inside the text content

• Display data in columns

• Display text with different transformations

Page 12: Advance text rendering in i os

Advance text Rendering in iOS

How to do it ?

Sohail Mohabbat Ali | iOS Mentor

Step 1: Prepare Attributed String

• First, you prepare a with all the parameters set (to the whole string or part of the string):

NSAttributedString * str = [[NSAttributedString alloc]

initWithString:@”Hello core text world!”

attributes:[NSDictionary

dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Helvetive" size:12.0],

(NSString *) kCTFontAttributeName]];

CFAttributedStringRef attrString =

(CFAttributedStringRef) str;

Page 13: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

Step 2: Create a CTFramesetter

• Now you can create a central object for Core Text typesetting:

, you will use it through the entire layout

process:

CTFramesetterRef framesetter =

CTFramesetterCreateWithAttributedString(attrString);

Page 14: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

Step 3: Create a CGPath

• Third, you add one or more (or other shapes) to form

a complete :

CGMutablePathRef path =

CGPathCreateMutable();

CGRect bounds = self.bounds;

CGPathAddRect(path, NULL, bounds);

Page 15: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

Step 4: Get the Frame

Use the and to create (s), then draw it in current context

CGContextRef context = UIGraphicsGetCurrentContext();

CTFrameRef frame =

CTFramesetterCreateFrame(framesetter,

CFRangeMake(0, 0),

path, NULL);

CTFrameDraw(frame , context);

Page 16: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

Step 5: Don’t forget to release them

Release everything you created with CGRelease.

CFRelease(attrString);

CFRelease(framesetter);

CFRelease(frame);

CFRelease(path);

Page 17: Advance text rendering in i os

Advance text Rendering in iOS

What is easy with Core Text

Sohail Mohabbat Ali | iOS Mentor

Step 5: Don’t forget to release them

Release everything you created with CGRelease.

CFRelease(attrString);

CFRelease(framesetter);

CFRelease(frame);

CFRelease(path);

Page 18: Advance text rendering in i os

Advance text Rendering in iOS

How to it ?

Sohail Mohabbat Ali | iOS Mentor

CGContextSetTextMatrix(context, CGAffineTransformIdentity);

CGContextTranslateCTM(context, 0, self.bounds.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

Page 19: Advance text rendering in i os

Advance text Rendering in iOS

Writing on Shapes

Sohail Mohabbat Ali | iOS Mentor

CGMutablePathRef path = CGPathCreateMutable();

CTFramesetterRef framesetter =

CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);

CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,

[attString length]), path, NULL);

CFRelease(framesetter);

CFRelease(path);

CTFrameDraw(theFrame, context);

CFRelease(theFrame);

Page 20: Advance text rendering in i os

Advance text Rendering in iOS

Writing on Shapes (Output)

Sohail Mohabbat Ali | iOS Mentor

Page 21: Advance text rendering in i os

Advance text Rendering in iOS

Writing on Shapes (Output)

Sohail Mohabbat Ali | iOS Mentor

Page 22: Advance text rendering in i os

Advance text Rendering in iOS

Playing with NSA Tributed String

Sohail Mohabbat Ali | iOS Mentor

With the help of function

we can format different chunks

of strings with different

attributes