zoukankan      html  css  js  c++  java
  • iOS.TextKit.02.文字图片混合排版

    1、案例如图

    2、代码

    TextKit02ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface TextKit02ViewController : UIViewController
    
    @property (nonatomic,strong) IBOutlet UITextView *textView;
    
    @property (nonatomic,weak) IBOutlet UIImageView *imageView;
    // 文本可以排版的区域
    @property (nonatomic,strong) NSTextContainer *textContainer;
    
    @end
    TextKit02ViewController.m
    #import "TextKit02ViewController.h"
    
    @interface TextKit02ViewController ()
    
    @end
    
    @implementation TextKit02ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);
        
        // 1、创建储存文本对象textStorage
        NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:self.textView.text];
        
        // 2、创建文字排版对象layoutManager
        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        
        // 3、创建文本排版的区域textContainer
        self.textContainer = [[NSTextContainer alloc] initWithSize:textViewRect.size];
        
        // 4、设置textStorage与layoutManager的关系
        [textStorage addLayoutManager:layoutManager];
        
        // 5、设置layoutManager与textContainer的关系
        [layoutManager addTextContainer:self.textContainer];
        
        // 6、重新构建原来控制器视图
        [self.textView removeFromSuperview];
        self.textView = [[UITextView alloc] initWithFrame:textViewRect textContainer:self.textContainer];
        [self.view insertSubview:self.textView belowSubview:self.imageView];
        
        // 7、设置textStorage中文本的风格
        [textStorage beginEditing];
        
        NSDictionary *attrsDic = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.textView.text attributes:attrsDic];
        [textStorage setAttributedString:attrStr];
        
        [textStorage endEditing];
        
        // 8、环绕路径
        self.textView.textContainer.exclusionPaths = @[[self translatedBezierPath]];
       
    }
    
    - (UIBezierPath *)translatedBezierPath
    {
        CGRect imageRect = [self.textView convertRect:self.imageView.frame fromView:self.view];
        UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageRect];
        return newPath;
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
     
  • 相关阅读:
    无法通过给定的扩展名确定设备类型
    biuld example_osgviewerGLUT遇到的error Link2019
    OpenGL渲染流水线
    设计模式总目录
    删除thumbs.db是提示正在使用
    COM技术内幕第十章笔记EXE中的服务器
    在高低版本之间互导max文件,以fbx格式为载体尤佳。
    参数值传递的本质
    VS 为什么要检查行尾的一致性?
    vs2005中的GL文件
  • 原文地址:https://www.cnblogs.com/cqchen/p/3776205.html
Copyright © 2011-2022 走看看