forked from wujunyang/MobileProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// NSString+Additions.h | ||
// MobileProject 用法可参照https://github.com/johnil/VVeboTableViewDemo | ||
// | ||
// Created by wujunyang on 16/7/28. | ||
// Copyright © 2016年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <CoreText/CoreText.h> | ||
|
||
@interface NSString (Additions) | ||
|
||
//计算大小带间距 | ||
- (CGSize)sizeWithConstrainedToWidth:(float)width fromFont:(UIFont *)font1 lineSpace:(float)lineSpace; | ||
- (CGSize)sizeWithConstrainedToSize:(CGSize)size fromFont:(UIFont *)font1 lineSpace:(float)lineSpace; | ||
|
||
//绘画文字 | ||
- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height andWidth:(float)width; | ||
|
||
- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height; | ||
|
||
@end |
100 changes: 100 additions & 0 deletions
100
MobileProject/Expand/Category/Other/NSString+Additions.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// NSString+Additions.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 16/7/28. | ||
// Copyright © 2016年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "NSString+Additions.h" | ||
|
||
@implementation NSString (Additions) | ||
|
||
- (CGSize)sizeWithConstrainedToWidth:(float)width fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{ | ||
return [self sizeWithConstrainedToSize:CGSizeMake(width, CGFLOAT_MAX) fromFont:font1 lineSpace:lineSpace]; | ||
} | ||
|
||
- (CGSize)sizeWithConstrainedToSize:(CGSize)size fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{ | ||
CGFloat minimumLineHeight = font1.pointSize,maximumLineHeight = minimumLineHeight, linespace = lineSpace; | ||
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)font1.fontName,font1.pointSize,NULL); | ||
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping; | ||
//Apply paragraph settings | ||
CTTextAlignment alignment = kCTLeftTextAlignment; | ||
CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[6]){ | ||
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}, | ||
{kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight}, | ||
{kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight}, | ||
{kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(linespace), &linespace}, | ||
{kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(linespace), &linespace}, | ||
{kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode} | ||
},6); | ||
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font,(NSString*)kCTFontAttributeName,(__bridge id)style,(NSString*)kCTParagraphStyleAttributeName,nil]; | ||
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes]; | ||
// [self clearEmoji:string start:0 font:font1]; | ||
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)string; | ||
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); | ||
CGSize result = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [string length]), NULL, size, NULL); | ||
CFRelease(framesetter); | ||
CFRelease(font); | ||
CFRelease(style); | ||
string = nil; | ||
attributes = nil; | ||
return result; | ||
} | ||
|
||
- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height andWidth:(float)width{ | ||
CGSize size = CGSizeMake(width, font.pointSize+10); | ||
CGContextSetTextMatrix(context,CGAffineTransformIdentity); | ||
CGContextTranslateCTM(context,0,height); | ||
CGContextScaleCTM(context,1.0,-1.0); | ||
|
||
//Determine default text color | ||
UIColor* textColor = color; | ||
//Set line height, font, color and break mode | ||
CTFontRef font1 = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize,NULL); | ||
//Apply paragraph settings | ||
CGFloat minimumLineHeight = font.pointSize,maximumLineHeight = minimumLineHeight+10, linespace = 5; | ||
CTLineBreakMode lineBreakMode = kCTLineBreakByTruncatingTail; | ||
CTTextAlignment alignment = kCTLeftTextAlignment; | ||
//Apply paragraph settings | ||
CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[6]){ | ||
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}, | ||
{kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight}, | ||
{kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight}, | ||
{kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(linespace), &linespace}, | ||
{kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(linespace), &linespace}, | ||
{kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode} | ||
},6); | ||
|
||
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font1,(NSString*)kCTFontAttributeName, | ||
textColor.CGColor,kCTForegroundColorAttributeName, | ||
style,kCTParagraphStyleAttributeName, | ||
nil]; | ||
//Create path to work with a frame with applied margins | ||
CGMutablePathRef path = CGPathCreateMutable(); | ||
CGPathAddRect(path,NULL,CGRectMake(p.x, height-p.y-size.height,(size.width),(size.height))); | ||
|
||
//Create attributed string, with applied syntax highlighting | ||
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes]; | ||
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)attributedStr; | ||
|
||
//Draw the frame | ||
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); | ||
CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,CFAttributedStringGetLength(attributedString)),path,NULL); | ||
CTFrameDraw(ctframe,context); | ||
CGPathRelease(path); | ||
CFRelease(font1); | ||
CFRelease(framesetter); | ||
CFRelease(ctframe); | ||
[[attributedStr mutableString] setString:@""]; | ||
CGContextSetTextMatrix(context,CGAffineTransformIdentity); | ||
CGContextTranslateCTM(context,0, height); | ||
CGContextScaleCTM(context,1.0,-1.0); | ||
} | ||
|
||
- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height{ | ||
[self drawInContext:context withPosition:p andFont:font andTextColor:color andHeight:height andWidth:CGFLOAT_MAX]; | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters