[无限互联]ios开发之FTCoreText的使用

2019-03-28 22:09

[无限互联]ios开发之FTCoreText的使用

本节要点:

1.FTCoreText的使用和功能 2.FTCoreText的结构简单说明 3.用法示例

FTCoreText的使用和功能

1.使用FTCoreText前须导入CoreText.framework框架才能使用

2.FTCoreTextView是一个开源的Objective-C界面组件,使用CoreText框架渲染可高度自定义的静态文本内容

FTCoreText的结构说明用法示例

注:FTCoreTextView主要负责静态文本界面内容,其中FTCoreTextStyle的属性来控制不同情况下绘制的形式

________________________________________________________________________________________________________________________ 1.FTCoreTextView 1.1 FTCoreTextView.h

重要的标签名和用于FTCoreTextView代理中

[objc] view plaincopy

1. 2. 3. 4. 5.

NSString *const FTCoreTextTagDefault ; NSString *const FTCoreTextTagImage ; NSString *const FTCoreTextTagBullet ; NSString *const FTCoreTextTagPage; NSString *const FTCoreTextTagLink ;

[objc] view plaincopy

1. 2. 3. 4. 5.

//用于代理

NSString *const FTCoreTextDataURL; NSString *const FTCoreTextDataName ; NSString *const FTCoreTextDataFrame ; NSString *const FTCoreTextDataAttributes;

1.1.1 FTCoreTextViewDelegate

[objc] view plaincopy

1. 2. 3. 4. 5. 6. 7.

@protocol FTCoreTextViewDelegate

@optional

- (void)coreTextView:(FTCoreTextView *)coreTextView receivedTouchOnData:(NSD

ictionary *)data;

- (void)coreTextViewfinishedRendering:(FTCoreTextView *)coreTextView; @end

1.2 FTCoreTextView.m 1.2.1 FTCoreTextCode

通过此类型来保存数据,便于增删检查等操作

[objc] view plaincopy

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

@interface FTCoreTextNode : NSObject

@property (nonatomic, assign) FTCoreTextNode *supernode; @property (nonatomic) NSArray *subnodes;

@property (nonatomic, copy) FTCoreTextStyle *style; @property (nonatomic) NSRange styleRange; @property (nonatomic) BOOL isClosed;

@property (nonatomic) NSInteger startLocation; @property (nonatomic) BOOL isLink; @property (nonatomic) BOOL isImage; @property (nonatomic) BOOL isBullet; @property (nonatomic) NSString *imageName;

- (NSString *)descriptionOfTree; - (NSString *)descriptionToRoot;

- (void)addSubnode:(FTCoreTextNode *)node;

- (void)adjustStylesAndSubstylesRangesByRange:(NSRange)insertedRange;

18. 19. 20. 21. 22. 23. 24. 25.

- (void)insertSubnode:(FTCoreTextNode *)subnode atIndex:(NSUInteger)index; - (void)insertSubnode:(FTCoreTextNode *)subnode beforeNode:(FTCoreTextNode *- (FTCoreTextNode *)previousNode; - (FTCoreTextNode *)nextNode; - (NSUInteger)nodeIndex;

- (FTCoreTextNode *)subnodeAtIndex:(NSUInteger)index; @end

)node;

1.2.2 部分方法介绍 *控制画图的方法

[objc] view plaincopy

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

[self.backgroundColor setFill]; CGContextFillRect(context, rect);

[self updateFramesetterIfNeeded];

CGMutablePathRef mainPath = CGPathCreateMutable();

if (!_path) {

CGPathAddRect(mainPath, NULL, CGRectMake(0, 0, self.bounds.size.widt } else {

CGPathAddPath(mainPath, NULL, _path); }

CTFrameRef drawFrame = CTFramesetterCreateFrame(_framesetter, CFRangeMak

if (drawFrame == NULL) {

if (_verbose) NSLog(@\, self.processedString); } else {

//draw images

if ([_images count] > 0) [self drawImages];

h, self.bounds.size.height));

e(0, 0), mainPath, NULL);

27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66.

if (_shadowColor) {

CGContextSetShadowWithColor(context, _shadowOffset, 0.f, _shadow }

CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); // draw text

CTFrameDraw(drawFrame, context); }

// cleanup

if (drawFrame) CFRelease(drawFrame); CGPathRelease(mainPath);

if ([_delegate respondsToSelector:@selector(coreTextViewfinishedRenderin [_delegate coreTextViewfinishedRendering:self]; } }

- (void)drawImages {

CGMutablePathRef mainPath = CGPathCreateMutable(); if (!_path) {

CGPathAddRect(mainPath, NULL, CGRectMake(0, 0, self.bounds.size.widt } else {

CGPathAddPath(mainPath, NULL, _path); }

CTFrameRef ctframe = CTFramesetterCreateFrame(_framesetter, CFRangeMake( CGPathRelease(mainPath);

NSArray *lines = (__bridge NSArray *)CTFrameGetLines(ctframe); NSInteger lineCount = [lines count]; CGPoint origins[lineCount];

CTFrameGetLineOrigins(ctframe, CFRangeMake(0, 0), origins);

FTCoreTextNode *imageNode = [_images objectAtIndex:0];

Color.CGColor);

g:)]) {

h, self.bounds.size.height));

0, 0), mainPath, NULL);

67. 68. 69.

for (int i = 0; i < lineCount; i++) { CGPoint baselineOrigin = origins[i];

//the view is inverted, the y origin of the baseline is upside down baselineOrigin.y = CGRectGetHeight(self.frame) - baselineOrigin.y;

CTLineRef line = (__bridge CTLineRef)[lines objectAtIndex:i]; CFRange cfrange = CTLineGetStringRange(line);

if (cfrange.location > imageNode.styleRange.location) { CGFloat ascent, descent;

CGFloat lineWidth = CTLineGetTypographicBounds(line, &ascent, &d

CGRect lineFrame = CGRectMake(baselineOrigin.x, baselineOrigin.y

CTTextAlignment alignment = (CTTextAlignment)imageNode.style.tex UIImage *img = nil;

if ([imageNode.imageName hasPrefix:@\]) {

NSData *myImgData = [NSData ftct_dataWithBase64EncodedString img = [UIImage imageWithData:myImgData]; } else {

img = [UIImage imageNamed:imageNode.imageName]; }

if (img) { int x = 0;

if (alignment == kCTRightTextAlignment) x = (self.frame.size if (alignment == kCTCenterTextAlignment) x = ((self.frame.si

CGRect frame = CGRectMake(x, (lineFrame.origin.y - img.size.

// adjusting frame

UIEdgeInsets insets = imageNode.style.paragraphInset;

70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101.

escent, NULL);

- ascent, lineWidth, ascent + descent);

tAlignment;

:[imageNode.imageName substringFromIndex:7]];

.width - img.size.width); ze.width - img.size.width) / 2);

height), img.size.width, img.size.height);


[无限互联]ios开发之FTCoreText的使用.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2012注册安全工程师考试题及答案(整理版)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: