Reprint of a rich text, quite practical

Source: Internet
Author: User
Tags transparent color set set

Article content outline
    • 1, the basic use of nsmutableattributedstring
    • 2. Simple package of nsmutableattributedstring
    • 3. Use open source code gobmarkuppaser to process Rich Text
    • 4, Uitextkit Introduction
    • 5. Related thinking of programming thought
Objective

Rich Text use case:

I also use the rich text here to implement a simple but also a common example:

For this setting, which is underlined in the ¥50, it is estimated that only rich text is best used.

    • In iOS or Mac OS x provided by Uikit, there are three display string controls:
      • Uilable,uitextfield,uitextview.

However, the control itself is very simple to display the text, usually only can control the font style, size, color, bold, italic, and so on, and for line spacing control, kerning, paragraph control and other advanced functions are powerless.

This is a very powerful text-typesetting framework coretext.framework.

The Coretext framework is a text engine that provides fine-grained control over text formatting and text layout based on IOS 3.2+ and OSX 10.5+. It's a good combination of UIKit and Core Graphics/quartz:

UIKit's UILabel allows you to add text by simply dragging in IB, but you can't change the color of the text and the words in it.

Core Graphics/quartz almost allows you to do anything that the system allows, but you need to calculate the position for each glyph and draw it on the screen.

Coretext is combining the two! You can fully control the location, layout, similar text size and color properties, Coretext will help you improve the other things?? Like text wrapping, font rendering, and so on.

However, thecoretext.framework itself is very large, the study cost is higher, the use is not very convenient, so generally not special needs, few people will use it .

With the release of the IOS6 API, the text display API is becoming more and more perfect, and one of the important updates is the support for attributedstring in Uitextfield,uitextview and Uilabel, leading control, kerning control, Advanced features such as paragraph control also don't have to use the esoteric coretext framework.

And IOS7 's release, Apple introduced Textkit,textkit is a fast and modern text typesetting and rendering engine.

Textkit does not have new classes, just in the original text display control is encapsulated, can be used in our favorite uilabel,uitextfield,uitextview and other controls inside, its most important role is to provide the program text layout and rendering functions.

Apple introduced Textkit not to replace the existing Coretext framework, although the main role of Coretext is also used for text typesetting and rendering, but it is an advanced and at the bottom of the technology, if we need to render the text content directly into the graphics context (graphics context), the best solution is to use coretext in terms of performance and usability considerations. And if we need to directly use some of the controls provided by Apple (such as Uitextview, Uilabel, and Uitextfield, etc.) to typeset text, then the API provided by Textkit in Uikit is undoubtedly more convenient and quick.

Textkit has very powerful features in word processing, and developers can customize and extend the Textkit. It is learnt that Apple has spent 2 years developing textkit, believing it is the gospel for many developers.

iOS nsmutableattributedstring use 1, nsmutableattributedstring basic use
    • Rich Text Note:
      • First set the first display, after the set, if and first set the style inconsistent, is overwritten, rich text settings have precedence.
      • Do not ignore the space is also a character.
      • It is recommended to use flexible nsmutableattributedstring and do not use nsattributedstring.

Summary of ideas for basic use of rich text

1、创建一个NSMutableAttributedString富文本对象(一般不用NSAttributedString)2、设置addAttribute属性    (1) addAttribute: 一个属性    (2) addAttributes: 一个存储多个属性的属性字典,比如这个字典可以是:        NSDictionary *attrDic = @{                 NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],                                               NSForegroundColorAttributeName: [UIColor blueColor]         }; 3、控件.attributedText = 富文本对象(和控件.txt = NSString文本对象不一样的)

Example Rich Text usage process:

Where attributedstring can set which properties, specifically, there are the following 21:

Nsfontattributename set font properties, default: Font: Helvetica (Neue) font size: 12NSForegroundColorAttributeNam set Font color, value is Uicolo R object, default value is black Nsbackgroundcolorattributename sets the region background color of the font, the value is Uicolor object, the default value is nil, the transparent color nsligatureattributename set the connection The NSNumber object (integer), 0 for no conjoined characters, 1 for the default conjoined character Nskernattributename to set the character spacing, the value is NSNumber object (integer), the positive spacing is widened, and the negative spacing narrows Nsstrikethroughstyleattributename set Strikethrough, value is NSNumber object (integer) nsstrikethroughcolorattributename set strikethrough color, value is Uicolor object, The default value is black Nsunderlinestyleattributename set underscore, value is NSNumber object (integer), enumeration value in constant Nsunderlinestyle, Similar to strikethrough nsunderlinecolorattributename sets the underline color, the value is the Uicolor object, the default value is black Nsstrokewidthattributename set the stroke width, the value is Nsnumb Er object (integer), negative fill effect, positive hollow effect nsstrokecolorattributename fill part of color, not font color, value is Uicolor object Nsshadowattributename set Set the Shadow property, the value is Nsshadow object Nstexteffectattributename sets the text special effect, takes the value to NSString object, currently only the plate printing effect is available: Nsbaselineoffsetattributenam E Set the baseline offset value to NSNumber (float), positive offset, negative bias nsobliquenessAttributeName set the glyph inclination, with a value of nsnumber (float), positive to the right, negative to the left Nsexpansionattributename set the text transverse stretch property, with a value of NSNumber (fl   OAT), positive values stretch text horizontally, negative values compress text Nswritingdirectionattributename set the direction of writing, write from left to right, or write from right to left Nsverticalglyphformattributename Set the text layout direction, the value is NSNumber object (integer), 0 for horizontal text, 1 for vertical text Nslinkattributename set link properties, click the browser to open the specified URL address nsattachmentattri  Butename Set the text attachment, the value is Nstextattachment object, commonly used in the text picture mix nsparagraphstyleattributename Sets the text paragraph typesetting format, takes the value to Nsparagraphstyle Object

With regard to how properties are used, it is possible to learn about unfamiliar properties through search engines.

You can also go directly to the bottom of the Uikit framework to query:

Note: It is the NSAttributedString.h under the Uikit framework, not the NSATTRIBUTEDSTRING.H under the foundation framework, although there are NSAttributedString.h under the foundation framework, but Foundati The on frame does not have any key for rich text, and it is completely different from the Uikit tool class.

2. Simple package of nsmutableattributedstring

Write ideas:

根据NSMutableAttributedString的基本使用,我们每次需要设置富文本,都需要创建NSMutableAttributedString并调用相同的add...方法。那么,就可以先创建一个类,并把这个add...方法抽离出来放进这个类中,类似MVC的抽离模型数据对象。这个类是属性模型对象,而不是数据模型对象哦。说法不一,但本质其实一样的。

Rich Text package source code code Baidu Cloud (continuous update) Download link: http://pan.baidu.com/s/1c04YC8o Password: 3i7e

3. Use open source code gobmarkuppaser to process Rich Text

First, there is a good learning blog about open source code gobmarkuppaser processing Rich Text: Http://www.cnblogs.com/YouXianMing/p/4269213.html is also a translator of the framework's original developer GitHub blog. It is worth learning English badly.

GitHub describes it more, but it's easier to use. As long as the following run the program, the source code also has the demo, first look at the source code to run the program effect:

Because this open source code gonmarkupparser processing Rich Text is more convenient, so later development project proposed to use.

Baidu Cloud Download Link: http://pan.baidu.com/s/1qWnFgQw Password: m2pe

4, Uitextkit Introduction

What is Uitextkit?

Textkit is new in the IOS7, which realizes the encapsulation of the coretext and is more convenient to use.

Although it is new, but also does not mean to be able to get started immediately-_-!!, Textkit can achieve the picture and text mixed results, very useful.

The implementation process is as follows:

Storage--LayoutManager--TextContainer-TextView

This..... Displaying a bunch of text is going to do so many things .....

One of the code instances is to implement highlighting some text:

For more detailed Uikit, refer to: http://www.cnblogs.com/YouXianMing/p/3767905.html and follow-up blog post.

When do I use Uitextkit?

Picture and text mix, but not when to achieve the text mixed with this, for example, some app Phoenix News client is not, but can use Uitextkit to achieve a slightly simple graphics and text mixing interface.

Just need to know that there is uitextkit such a thing, in fact, this uitextkit can be used to implement a simple e-book reader.

Bézier n. Bezier; Bezier curve exclusion N. Reject, exclude Small project implementation ebook: booktextview instance effect dynamic diagram: Small project implementation ebook: Code on Viewcontroller in Booktextview
 1 #import "ViewController.h" 2 #import "Paragraphattributes+constructor.h" 3 #import "ExclusionView.h" 4 #import "Booktex                            TView.h "5 6 #define Width [UIScreen mainscreen].bounds.size.width 7 #define Height  [UIScreen mainscreen].bounds.size.height 8 9 @interface viewcontroller () <uitextviewdelegate>10 One @property (strong, nonatomic) Booktextview *bookview;12 @end14 @implementation ViewController16-(void) viewd idload {[Super viewdidload];19 20 21]//Read text NSString *text = [NSString stringwithcontentsoffile: [Nsbundle.mainbundle urlforresource:@ "Lorem" withextension:@ "TXT"].path23 E Ncoding:nsutf8stringencoding24 error:nil];25 26 27//Initialize Boo kView28 Self.bookview = [[Booktextview alloc] Initwithframe:cgrectmake (Ten, Width-20, Height -)];29 self.bookview.textstring = text;30 31//Set paragraph style by self.bookView.paragraphAttributes = [Paragraphattributes Qing KEBENGYUE];33 34//Set Rich Text Self.bookView.attributes = @[[configattributedstring Foregroundcolor:[[ui                                                                             Color Blackcolor] Colorwithalphacomponent:0.75f]36 Range:nsmakerange (0, 9)],37 [configattributedstring Font:[uifont Fontwithna Me:qingkebengyue size:22.f]38 range:nsmakerange (0, 9)]];3  9 40//Load picture Exclusionview *exclusionview = [[Exclusionview alloc] Initwithframe:cgrectmake (150.F, 195, 320, ];42 self.bookView.exclusionViews = @[exclusionview];43 Uiimageview *imageview = [[Uiimageview alloc] I nitwithframe:exclusionview.bounds];44 imageview.image = [UIImage imagenamed:@ "Demo"];45 [Exclusionvie W Addsubview:imageview];46 47 48//Build view49 [Self.bookview buildwidgetview];50 [Self.view addsubview:self.bookview];51 52 53//Delay 0.01s execution [self performselector: @selector (event) withobject:nil56 aft erdelay:0.01];57}58-(void) event {[Self.bookview movetotextpercent:0.00];61}62 @end
Project DOWNLOAD Link: Http://pan.baidu.com/s/1pJEfAhP Password: 2aj6 5. Thinking about programming thought

In an instance of Booktextview that uses Uikit, because a custom view is used, the rich text needs to be displayed on the custom view. Because rich text can be set up by a number of different property values, such as: Font size, text foreground color, background color and so on, you need to pull out to create a new property-value model object, each object stores an attribute-value, and then add the property's Nsarray in the custom view, Used to store and override the Set method to load the passed-in property-value model object, and then pass the text object corresponding to the property variable Rich text object that is not set by the add these property-value model objects stored values. Of course, this booktextview internal because of the use of Uikit so is through the nstextstorage *storage through the method add load Nsarray object stored Property-value.

In Hystringattribute instances, because only the rich-text objects corresponding to the NSString object are set directly on the property, and finally added to a control such as a label, because the requirements of the simple package are considered, all attributes are specifically abstracted into an abstract class that requires protocol constraints. , and this abstract class is then specifically derived from specific property objects, such as a Font object, a foreground color object, a background color object ... There are only three of them, and there are plenty of time to expand as needed. When used, it is necessary to use the method of category extension to make it easy to:<1> one is the rich text class expands out the method, this method implements the concrete Property object which passes through the parameter, then loads this object, sets the rich Text property. <2> one is the NSString class that expands the method, which directly creates and loads the concrete Property object passed in through the arguments, and then returns a rich text object that has the rich Text property set in its own instance object self through rich text.

Reprint Annotated Source: http://www.cnblogs.com/goodboy-heyang/p/5143135.html Respect the fruits of labor.

Reprint of a rich text, quite practical

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.