IOS Quartzcore The eighth chapter--catextlayer application

Source: Internet
Author: User

The user interface is not built from a single image. A well-designed icon can be a good representation of the intent of a button or control, but sooner or later you need a nice old-fashioned text label.

If you want to display text inside a layer, you can use the layer proxy to write the string directly to the layer's content using the core graphics (this is the essence of Uilabel). If you go over a layer-hosted view and work directly on the layer, it's pretty tedious. You want to create a class that works like a layer proxy for each layer that displays text, and logically determine which layer needs to be displayed, not to mention a series of messy things, such as different fonts, colors, and so on.

Fortunately, none of this is necessary, and Core animation provides a calayer subclass Catextlayer, which contains almost all of the drawing features of Uilabel in the form of layers, and provides some additional new features.

Similarly, catextlayer are rendered much faster than Uilabel. Few people know that in iOS 6 and earlier, Uilabel is actually drawn through WebKit, which results in great performance stress when there is a lot of text. The Catextlayer uses the core text and renders it very quickly.

Let's try to use Catextlayer to display some text. The code in Listing 6.2 implements this function, as shown in result 6.2.

Listing 6.2 uses Catextlayer to implement a Uilabel

@interfaceViewcontroller () @property (nonatomic, weak) Iboutlet UIView*LabelView;@end @implementationViewcontroller- (void) viewdidload{[Super Viewdidload];//Create a text layerCatextlayer *textlayer =[Catextlayer layer]; Textlayer.frame=Self.labelView.bounds; [Self.labelView.layer Addsublayer:textlayer]; //Set Text attributesTextlayer.foregroundcolor =[Uicolor Blackcolor].  Cgcolor; Textlayer.alignmentmode=kcaalignmentjustified; textlayer.wrapped=YES;//Choose a FontUifont *font = [Uifont systemfontofsize: the]; //Set Layer FontCfstringref FontName =(__bridge cfstringref) font.fontname; Cgfontref Fontref=Cgfontcreatewithfontname (FontName); Textlayer.font=Fontref; Textlayer.fontsize=font.pointsize; Cgfontrelease (FONTREF); //Choose some textNSString *text =@"Lorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar \ Leo. Nunc quis Nunc at Mauris pharetra condimentum ut ac neque. Nunc elementum, libero ut porttitor dictum, diam odio congue lacus, vel \ Fringilla sapien diam at Purus. Etiam suscipit pretium nunc sit amet \ lobortis"; //Set Layer textTextlayer.string=text;} @end

Figure 6.2 using Catextlayer to display a plain text label

If you look at the text carefully, you will find a strange place: The text is somewhat pixelated. This is because it is not rendered in the retina, and the second chapter refers to the Contentscale attribute, which determines what resolution the layer's content should be rendered in. Contentsscale does not care about the stretch factor of the screen and always defaults to 1.0. If we want to display the text in the quality of the retina, we have to manually set the Contentsscale property of the Catextlayer, as follows:

Textlayer.contentsscale = [UIScreen mainscreen].scale;

This solves the problem (6.3)

Figure 6.3 Setting Contentsscale to match the screen

The Catextlayer Font property is not a uifont type, but a cftyperef type. This allows you to decide whether the font property should be in the Cgfontref type or the Ctfontref type (Core text font), depending on your specific needs. The font size is also set separately with the FontSize property, because Ctfontref and cgfontref do not include the point size as Uifont. This example will show you how to convert Uifont to Cgfontref.

In addition, the string property of Catextlayer is not the NSString type you imagined, but the ID type. This way you can use NSString or nsattributedstring to specify the text (note that nsattributedstring is not a subclass of NSString). The attributed string is the mechanism that iOS uses to render the font style, which determines the original information of a string within a specified range, such as font, color, character weight, italic, and so on, in a specific way.









Catextlayer *lary = [Catextlayer layer];

Lary.backgroundcolor = [Uicolor Bluecolor]. Cgcolor;
lary.string = @ "Dasfasa";
Lary.bounds = CGRectMake (0, 0, 320, 20);
Lary.position = Self.view.center;
Lary.contentsscale = 5;
Lary.font = @ "HIRAKAKUPRON-W3"; The name of the font is not Uifont
Lary.fontsize = 12.f; Size of font
Lary.alignmentmode = kcaalignmentcenter;//Font Alignment
Lary.foregroundcolor = [Uicolor Redcolor]. cgcolor;//the color of the font
[Self.view.layer Addsublayer:lary];

IOS Quartzcore The eighth chapter--catextlayer application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.