IOS development diary 21-7.0 after the text mixing, ios21-7.0

Source: Internet
Author: User

IOS development diary 21-7.0 after the text mixing, ios21-7.0

Today, the blogger has a text-and-text layout demand and encountered some difficulties. I would like to share with you the hope to make common progress.

Before iOS7.0, there were two main ways to mix text and text: 1. WebView + js 2. coreText

After iOS7.0, Apple provided a new encapsulation to make text-and-image mixing easier, that is, the third method: 3. TextKit

Today I will share with you the three methods of text and text Mixing

1. The webview + js method is actually very simple. The code below will be posted for your own research.

Remove the white edges from and from the webView when scrolling.

- (void)clearWebViewBackground:(UIWebView *)webView{    UIWebView *web = webView;    for (id v in web.subviews) {        if ([v isKindOfClass:[UIScrollView class]]) {            [v setBounces:NO];        }    }}

Set proxy

// Set proxy self. myWebView. delegate = self;

Add a view to load webview

# Pragma mark load WebView-(void) loadMyWebView {NSString * title = @ "Han's wonderful gold-absorbing 3 secret in the future"; NSString * linkStr = [NSString stringWithFormat: @ "<a href = '% @'> my blog </a> <a href = '% @'> original </a>", @ "http://blog.csdn.net/wildcatlele ", @ "http://jincuodao.baijia.baidu.com/article/26059"]; NSString * p1 = @ "Han" will be in the future "the ability to suck gold surprised me! In August 12, the box office of the film successfully broke through the 0.6 billion mark. Moreover, the number of slices is still above 10, and the pace of daily acquisition is steadily reaching 0.7 billion. "; NSString * p2 = @" you must know that "coming soon" is not a mainstream type of film, but a literary film. Unlike the little age, it is a mainstream commercial idol movie. "; NSString * image1 = [NSString stringWithFormat: @"  ", @" http://nvren.so/uploads/allimg/c140801/140DR4554L40-YB9.jpg "]; NSString * image2 = [NSString stringWithFormat: @ " \ is very cruel and worth your reference. Han: What are the secret weapons in "coming soon"? Welcome to the "coming soon" team or related parties, and leave a message directly. Unexpectedly, you can send Li wanqiang a signed book "sense of participation. "; // Initialization and html string NSString * htmlURlStr = [NSString stringWithFormat: @ "<body style = '

Implement proxy methods (process connection click events)

-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSString * urlStr = request. URL. absoluteString; NSLog (@ "url: % @", urlStr); // null. if ([urlStr is1_tostring: @ "about: blank"]) {return YES;} // set the View Controller after clicking LvesOriginalController * origin1c = [[LvesOriginalController alloc] init]; origin1c. originUrl = urlStr; // Set Request connection // jump to the Controller after clicking and load webview [self. navigationController pushViewController: origin1c animated: YES]; return NO;} // you can specify the webViewDidFinishLoad (UIWebView *) webView {NSInteger height = [[webView progress: @ "document. body. offsetHeight; "] intValue]; NSString * javascript = [NSString stringWithFormat: @" window. scrollBy (0, % d); ", height]; [webView stringByEvaluatingJavaScriptFromString: javascript];}

2. coreText is the underlying layer of text and text mixing. If you want to write your own text layout engine, you can use coreText

-(Void) drawRect :( CGRect) rect {

// Drawing code

[Super drawRect: rect];

// 1. Get the context of the currently drawn canvas, which is used to draw the content on the canvas later

CGContextRef context = UIGraphicsGetCurrentContext ();

// 2. Flip the coordinate system up and down. For the underlying rendering engine, the lower left corner of the screen is the (0, 0) coordinate. For the upper-level UIKit, the upper-left corner is the (0, 0) coordinate. Therefore, we follow the UIKit to describe the subsequent coordinate system, so we will first perform the up/down flip operation on the coordinate system. After the flip, the coordinates of the bottom layer and the upper layer (0, 0) overlap.

CGContextSetTextMatrix (context, CGAffineTransformIdentity );

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

CGContextScaleCTM (context, 1.0,-1.0 );

// 3.

CGMutablePathRef path = CGPathCreateMutable ();

CGPathAddRect (path, NULL, self. bounds );

// 4. Create a drawn area. CoreText itself supports various text typographical areas. Here we simply use the UI of UIView as the layout area.

NSAttributedString * attString = [[NSAttributedString alloc] initWithString: @ "Hello World! "];

CTFramesetterRef framestter = CTFramesetterCreateWithAttributedString (CFAttributedStringRef) attString );

CTFrameRef frameOfCT = CTFramesetterCreateFrame (framestter, CFRangeMake (0, [attString length]), path, NULL );

// 5.

CTFrameDraw (frameOfCT, context );

// 6.

CFRelease (frameOfCT );

CFRelease (path );

CFRelease (framestter );

}

Http://www.cocoachina.com/industry/20140521/8504.html

Http://www.tuicool.com/articles/jEBrq2B

 

3. TextKit is an encapsulation of coreText. Apple provides a new API that allows us to easily layout texts.

// Create a Rich Text

NSMutableAttributedString * sttriAS = [[NSMutableAttributedString alloc] initWithString: @ "HAHAHA 1234567890"];

// Modify the style of different texts in Rich Text

[SttriAS addattriename: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange (0, 3)];

[SttriAS addattriename: NSFontAttributeName value: [UIFont systemFontOfSize: 20] range: NSMakeRange (0, 3)];

// Set the number to red.

[SttriAS addattriename: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange (3, 10)];

[SttriAS addattriename: NSFontAttributeName value: [UIFont systemFontOfSize: 30] range: NSMakeRange (3, 10)];

// Add an image

NSTextAttachment * attch = [[NSTextAttachment alloc] init];

Attch. bounds = CGRectMake (0, 0, 32, 32 );

Attch. image = [UIImage imageNamed: @ "8"];

// Create rich text with images

NSAttributedString * string = [NSAttributedString attributedStringWithAttachment: attch];

[SttriAS appendAttributedString: string];

// Display

Self. asdfhLabel. attributedText = sttriAS;

Self. asdhagTextView. attributedText = sttriAS;

 

Http://www.cocoachina.com/industry/20131126/7417.html

Http://www.cocoachina.com/ios/20131028/7250.html

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.