iOS for simple graphics and text blending effect

Source: Internet
Author: User

iOS for simple graphics and text blending effect

In a lot of news or have text display of the application now will appear in the picture mixed interface such as NetEase News, at first glance to look similar to a webpage, in fact, this effect is not by UIWebView load page implementation. Now share a relatively simple way to implement

The IOS SDK provides us with a complete set of text-layout development components: Coretext. The Coretext library provides a number of tools to manipulate text, such as Ctfont, Ctline, Ctframe, and so on. These tools allow you to manipulate each paragraph in each line of text font.

In this example, the default picture is in the top right, and the width is the same for both aesthetic and development convenience settings.

    1. First, you need to introduce the Coretext library

Add the #import header file to the class file that you want to use.

    1. Set parameters for text

Create a Nsmutableattributedstring object that contains the text string you want to present. This makes it possible to set it up. Create a Ctfont object from the Ctfontcreatewithname function and set it using the AddAttribute method of the Nsmutableattributedstring object. A similar method can be used to set the word spacing.

How to set the way and line spacing: [CPP] View plain copy print?

// 文本对齐方式       CTTextAlignment alignment = kCTLeftTextAlignment;       CTParagraphStyleSetting alignmentStyle;       alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;       alignmentStyle.valueSize = sizeof(alignment);       alignmentStyle.value = &alignment;    // 创建设置数组               CTParagraphStyleSetting settings[] ={alignmentStyle};   CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1); 

Also use AddAttribute to set the string object. This method can also set the line spacing, segment spacing and other parameters.

    1. Calculates the height of the picture. Images can be displayed using Uiimageview. It's easy to get the total height per picture.

    2. Because the width of the picture is fixed, the number of words shortened per line can be calculated. Text length is shortened as long as the overall height of the text is lower than the total height of the image. Use the Cttypesettersuggestlinebreak function to calculate the number of words in each line dynamically, because each line inside the text, punctuation, numbers, letters are not the same so the number of words can be displayed is certainly different, so need to make such calculations. This loops until the end of the text, so you know how many lines are there. The total text height is then calculated based on the font height and line spacing, and if the text height is greater than the total picture height, the frame height of the display area is the height of the text and vice versa.

    3. Draw text:

Set the interval in which each line draws text: [CPP] View plain copy print?

CFRange lineRange = CFRangeMake(currentIndex, lineLength);   建立文本行对象   CTLineRef line = CTTypesetterCreateLine(typeSetter, lineRange);   CGFloat x = [self textOffsetForLine:line inRect:self.bounds];   // 设置一行的位置 CGContextSetTextPosition(context, x, y); // 绘制一行文字 CTLineDraw(line, context); 
    1. Other Features:

After the completion of the text drawing function can be added to adjust the text size of the function, and the image of the enlarged function.

The text size can be achieved by redrawing the text directly after setting the font size.

Image magnification You can add a new uiimageview to the view to show the enlarged picture, and add animation

iOS for simple graphics and text blending effect

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.