IOS UI-Rich Text summary and iosui-Summary

Source: Internet
Author: User

IOS UI-Rich Text summary and iosui-Summary
Content outline

  • 1. Basic use of NSMutableAttributedString
  • 2. Simple encapsulation of NSMutableAttributedString
  • 3. Use open-source GOBMarkupPaser to process Rich Text
  • 4. UITextKit Introduction
  • 5. Thoughts on programming ideas
Preface

Rich Text use case:

Here I also use Rich Text to implement simple but common examples:

It is estimated that only rich text is the best choice for the above setting.

 

  • In IOS or Mac OS X, the string control provided by UIKit is displayed in three ways:
    • UILable, UITextField, UITextView.

However, these controls display the text in a single way. They can only control the font style, size, color, bold, italic, and so on, paragraph control and other advanced functions are powerless.

A very powerful text Layout FrameworkCoreText. framework.

CoreText framework is a text engine based on iOS 3.2 + and OSX 10.5 + that can precisely control text formats and text la S. It works well with the combination of UIKit and Core Graphics/Quartz:

UILabel of UIKit allows you to add text by simple drag in IB, but you cannot change the color of the text and the words in it.

Core Graphics/Quartz allows you to do almost anything the system allows, but you need to calculate positions for each glyph and draw on the screen.

CoreText is combining the two! You can completely control the location, layout, text size and color attributes by yourself. CoreText will help you complete other things ?? Similar to text wrapping and font rendering.

However,CoreText. framework itself is very huge, the learning cost is high, it is not very convenient to use, so it is generally not a special need, few people will use it.

With the release of iOS6 API, the text display API is becoming more and more perfect. One important update is that AttributedString support is added to UITextField, UITextView, and UILabel to implement line spacing control, advanced features such as padding control and paragraph control do not need to use the esoteric CoreText framework.

With the release of iOS7, Apple introduced TextKit, which is a fast and modern text layout and rendering engine.

TextKit does not add any new classes. It only encapsulates the original text display controls and can be used in our favorite UILabel, UITextField, UITextView, and other controls, its main function is to provide the function of text layout and rendering for the program.

Apple's introduction of TextKit does not aim to replace the existing CoreText framework. Although CoreText is mainly used for text layout and rendering, it is an advanced and bottom layer technology, if we need to render the text content directly to the graphic context, the best solution is to use CoreText in terms of performance and ease of use. However, if we need to directly use some controls provided by Apple (such as UITextView, UILabel, and UITextField) to typeset text, then the API provided by TextKit in UIKit is undoubtedly more convenient and quick.

TextKit has very powerful functions in text processing, and developers can customize and expand TextKit. It is reported that Apple has spent two years developing TextKit. I believe this is a good news for many developers.

Usage of NSMutableAttributedString in IOS 1. Basic usage of NSMutableAttributedString
  • Note:
    • The settings are displayed first and then set later. If they are inconsistent with the first style, they will be overwritten. The rich text settings are sorted in order.
    • Do not ignore spaces.
    • We recommend that you use flexible and easy-to-use NSMutableAttributedString instead of NSAttributedString.

Introduction to the basic usage of rich texts

1. Create an NSMutableAttributedString Rich Text object (generally NSAttributedString is not used) 2. Set addAttribute attribute (1) addAttribute: one attribute (2) addAttributes: one attribute dictionary that stores multiple attributes, for example, this dictionary can be: NSDictionary * attrDic =@{ NSFontAttributeName: [UIFont fontWithName: @ "Zapfino" size: 15], NSForegroundColorAttributeName: [UIColor blueColor]}; 3. attributedText = rich objects (different from the control .txt = NSString Text object)

Example of Rich Text use process:

Sample Code:

View Code

Which attributes can be set for AttributedString? Specifically, there are 21 attributes:

NSFontAttributeName sets the font attribute. Default Value: Font: Helvetica (Neue) font size: 12 NSForegroundColorAttributeNam sets the font color. The default value is black NSBackgroundColorAttributeName, the value is a UIColor object, the default value is nil, and the transparent NSLigatureAttributeName sets the ing attribute. The value is an NSNumber object (integer), and 0 indicates that no conjoined characters exist, 1 indicates that the default NSKernAttributeName character is used to set the Character spacing. The value is an NSNumber object (integer), the positive value spacing is widened, and the negative value spacing is narrowed by NSStrikethroughStyleAttributeName. The value is an integer of NSNumber) NSStrikethroughColorAttributeName: Specifies the strikethrough color. The value is a UIColor object. The default value is black. The value is an NSNumber object (integer). The value in the constant NSUnderlineStyle is similar to the value in NSUnderlineColorAttributeName, the value is a UIColor object, and the default value is black NSStrokeWidthAttributeName. Set the stroke width to NSNumber object (integer), negative value filling effect, positive value hollow effect NSStrokeColorAttributeName fill part of the color, not the font color, the value is set to the NSShadowAttributeName of the UIColor object. The value is set to the NSTextEffectAttributeName of the NSShadow object to set the special text effect. The value is NSString. Currently, only the graphic printing effect is available, the value is NSNumber (float), the positive value is on the top, and the negative value is on the bottom of the NSObliquenessAttributeName. The font skew is set to NSNumber (float), and the positive value is on the right and the negative value is on the left, the value is NSNumber (float), and the positive value is horizontal text. The negative value is horizontal compressed. The nswritingdireattributbutename parameter sets the text writing direction, from left to right, or from right to left. NSVerticalGlyphFormAttributeName sets the text layout direction, the value is an NSNumber object (integer), 0 indicates horizontal text, 1 indicates the vertical text NSLinkAttributeName sets the link property, and then calls the browser to open the specified URL NSAttachmentAttributeName to set the text attachment. The value is the NSTextAttachment object, it is often used in text image mixing NSParagraphStyleAttributeName to set the text section layout format. The value is a NSParagraphStyle object.

For details about how to use attributes, you can use search engines to understand unfamiliar attributes.

You can also directly query at the underlying layer of the UIKIt framework:

Note: It is the NSAttributedString in the UIKit framework. h, instead of NSAttributedString under the Foundation framework. h. Check whether NSAttributedString exists in the Foundation framework. h. However, the Foundation framework does not have any keys in fuwen, and they are completely different from those in UIKit.

2. Simple encapsulation of NSMutableAttributedString

Writing ideas:

According to the basic usage of NSMutableAttributedString, every time we need to set rich text, we need to create NSMutableAttributedString and call the same add... method. Then, you can create a class and extract the add... method into the class, similar to the MVC model data object. This class is an attribute model object, not a data model object. There are different arguments, but they are actually the same in nature.

Rich Text encapsulation source code Baidu cloud (Continuous updates) download link: http://pan.baidu.com/s/1c04YC8o password: 3i7e

 

3. Use open-source GOBMarkupPaser to process Rich Text

First of all, there is a good learning blog on how to process rich texts with open-source code GOBMarkupPaser: http://www.cnblogs.com/youxianming/p/4109213.htmlis also the original framework developer's githubbo translation. It is worth learning about poor English.

Although github described a lot, it is easy to use. Next, you just need to run the program and there is a demo in the source code. First, let's take a look at the effect of the program running in the source code:

This open-source code GONMarkupParser is easier to process rich texts, so we recommend that you use it for future development projects.

Baidu cloud download link: http://pan.baidu.com/s/1qWnFgQw password: m2pe

4. UITextKit Introduction

What is UITextKit?

TextKit is newly released in iOS7, which encapsulates CoreText and makes it easier to use.

Although it is new, it does not mean you can get started immediately -_-!!, TextKit can achieve the text-and-text mixing effect, which is easy to use.

The implementation process is as follows:

Storage --> layoutManager --> textContainer --> textView

So many things are required to display a string of text .....

A code example is to highlight some text:

For more information about UIKit, see: http://www.cnblogs.com/YouXianMing/p/3767905.html and follow-up blog.

 

When Will UITextKit be used?

Text-to-text mixing, but it is not always necessary to achieve text-to-text mixing, for example, some app Phoenix News clients are not, but you can use UITextKit to implement a simple text-to-text mixing interface.

You only need to know about the UITextKit. In fact, this UITextKit can be used to implement a simple e-book reader.

Betiller n. Bezier; besell curve exclusion n. Reject, exclude small projects to implement ebook: BookTextView instance effect Dynamic Graph: small projects to implement ebook: Code on ViewController in BookTextView
1 # import "ViewController. h "2 # import" ParagraphAttributes + Constructor. h "3 # import" ExclusionView. h "4 # import" BookTextView. h "5 6 # define Width [UIScreen mainScreen]. bounds. size. width 7 # define Height [UIScreen mainScreen]. bounds. size. height 8 9 @ interface ViewController () <UITextViewDelegate> 10 11 @ property (strong, nonatomic) BookTextView * bookView; 12 13 @ end14 15 @ implementation ViewController16 17-(void) viewDidLoad {18 [super viewDidLoad]; 19 20 21 // read the text 22 NSString * text = [NSString stringWithContentsOfFile: [NSBundle. mainBundle URLForResource: @ "lorem" withExtension: @ "txt"]. path23 encoding: NSUTF8StringEncoding24 error: nil]; 25 26 27 // initialize bookView28 self. bookView = [[BookTextView alloc] initWithFrame: CGRectMake (10, 10, Width-20, Height-20)]; 29 self. bookView. textString = text; 30 31 // set the paragraph style 32 self. bookView. paragraphAttributes = [ParagraphAttributes qingKeBengYue]; 33 34 // set Rich Text 35 self. bookView. attributes = @ [[ConfigAttributedString foregroundColor: [[UIColor blackColor] gradient: 0.75f] 36 range: NSMakeRange (0, 9)], 37 [ConfigAttributedString font: [UIFont fontWithName: bytes size: 22.f] 38 range: NSMakeRange (0, 9)]; 39 40 // load the image 41 ExclusionView * exclusionView = [[ExclusionView alloc] initWithFrame: CGRectMake (150.f, 195,320,150)]; 42 self. bookView. exclusionViews = @ [exclusionView]; 43 UIImageView * imageView = [[UIImageView alloc] initWithFrame: exclusionView. bounds]; 44 imageView. image = [UIImage imageNamed: @ "demo"]; 45 [exclusionView addSubview: imageView]; 46 47 48 // construct view49 [self. bookView buildWidgetView]; 50 [self. view addSubview: self. bookView]; 51 52 53 // latency 0.01s execution 54 [self defined mselector: @ selector (event) 55 withObject: nil56 afterDelay: 0.01]; 57} 58 59-(void) event {60 [self. bookView moveToTextPercent: 0.00]; 61} 62 63 @ end
Project download link: http://pan.baidu.com/s/1pJEfAhP password: 2aj6 5, related ideas of Programming

In the BookTextView instance that uses UIKit, because custom View is used, rich text must be displayed on the custom View. Because Rich Text settings can be set by multiple different attribute values, such as font size, text foreground color, and background color, you need to extract and create a new property-value model object, each object stores a property-value, and then adds the property NSArray to the custom View to store and override the set Method to load the passed property-value model object, then, you can add these attribute-value model object storage values to the text object without setting attributes. Of course, this BookTextView uses UIKit internally, so it uses NSTextStorage * storage to load the attribute-value of NSArray Object storage through the traversal Method add.

In the HYStringAttribute instance, you only need to directly set the attributes of the Rich Text object corresponding to the NSString object and add it to the Label and other controls for display, because the simple encapsulation needs are considered, therefore, all attributes are abstracted into an abstract class that requires protocol constraints. Then, this abstract class derives a specific attribute object, such as a Font object and a foreground object, background Color object ..... there are only three implementations, and there is still a lot of time to expand as needed. When using this method, you must use the class Extension Method for ease of use: <1> one is the rich text class extension method, which implements the specific attribute object passed through the parameter, then load the object and set the Rich Text attribute. <2> one is the NSString class extension method. This method directly creates and loads the Instance Object self through rich text, and the specific property object passed in through parameters, then return the Rich Text object with the rich text attribute set.

Reprinted to indicate the source: http://www.cnblogs.com/goodboy-heyang/p/5143135.html respect labor results.

Related Article

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.