用TextKit实现表情混排

iOS中的富文本技术(1)-TextKit简介 前言 最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结。初步打算写两篇,这是第一篇,主要介绍iOS7新出的TextKit的简单实用。 简介 TextKit是iOS7新推出的文字排版技术,使用TextKit可以很方便的实现富文本、表情混排和图文混排等效果。TextKit中的几个关键的类: NSAttributeStri 阅读详情

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。     TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的UILabel,UITextField,UITextView里面就可以使用了。

    

     1.NSAtrributedString

          这是所有TextKit的载体,所有的信息都会输入到NSAttributedString里面,然后将这个String输入到Text控件里面就可以显示了。

     2.NSTextAttachment

     iOS7新增的类,作为文本的附件,可以放文件,可以放数据,以 NSAttachmentAttributeName这个key放入NSAttributedString里面,在表情混排这里,我们将放入image。

  

   3.重载NSTextAttachment

     本来是可以直接使用NSTextAttachment,但是我们需要根据文字大小来改变表情图片的大小,于是我们需要重载NSTextAttachment,NSTextAttachment实现了NSTextAttachmentContainer,可以给我们改变返回的图像,图像的大小。

     重载NSTextAttachment代码:

          

      
?
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
27
28
29
30
31
32
33
           @interface MMTextAttachment : NSTextAttachment
 
           {
 
     
 
           }
 
 
 
           @end
 
 
 
           @implementation MMTextAttachment
 
 
 
           //I want my emoticon has the same size with line's height
 
           - (CGRect)attachmentBoundsForTextContainer:( NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:( NSUInteger )charIndex NS_AVAILABLE_IOS (7_0)
 
           {
 
               return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height );
 
           }
 
 
 
 
 
                     @end


     4.在你的代码里面加入:

      
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NSMutableAttributedString * string = [[ NSMutableAttributedStringalloc ] initWithString:@ "123456789101112"   attributes: nil ] ;
     
 
     MMTextAttachment * textAttachment = [[ MMTextAttachment alloc ] initWithData: nil ofType: nil ] ;
 
     UIImage * smileImage = [ UIImage imageNamed:@ "a.jpg" ]  ;   //my emoticon image named a.jpg
 
     textAttachment.image = smileImage ;
 
 
 
     NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;
 
     [ string insertAttributedString:textAttachmentString atIndex:6 ] ;
 
     
 
     _textView.attributedText = string ;



     最后,最厉害的一点是,从此以后,可以在输入的同时也可以编辑图片了。


描述:示例的代码

附件: TextKitTest.zip

http://www.cocoachina.com/bbs/read.php?tid=144272&fpage=14

富文本(图文混排)—— TextKit & CoreText 富文本 图文混排 3种方式: 1.webView 使用简单 负载大 2.CoreText 性能好,功能强大 使用起来复杂 3.TextKit iOS7以后 是对CoreText做了封装,集成在UIKit框架中 使用比较简单,功能有限 //NSAttributedString.h 中文本属性key的说明 /* NSFontAttributeName 设置字体属性,默认值:字体 阅读详情

相关推荐

ESP32连接PDM麦克风操作指南:新手入门必看

手把手教你如何将ESP32与PDM麦克风连接,采集音频数据并用于音频分类应用。内容涵盖硬件接线、代码配置及实际调试技巧,适合初学者快速上手esp32在声音识别场景中的使用。

weixin_42590539的博客 1336

ios textkit html,iOS系列教程之TextKit实现图文混排读后记

前两天看搜狐家明哥写的《TextKit实现图文混排》 今晚回家看了下API发现了一个更加取巧的实现方式.可以直接将后台返回的html富文本用textView显示出来.记得两年前当时做这个的时候还是借助了笨重的webview.>Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,下面是我分别用UIT...

weixin_39679664的博客 300

嵌入式工程师必学(9):USB 3.0协议理解

USB3.0和USB2.0相比,在线缆上从4根线(Vbus、GND、D+、D-)变为了8根线(Vbus、GND、D+、D-、SSRX+\SSRX-\SSTX+\SSTX-),可见增加的4根线都是和超速相关的,新增加的超速有两类,分别为:超速Gen1(5.0Gbps),超速+Gen2(10Gbps),还兼容USB2.0的3类速率:low-speed(1.5Mbps),full-speed(12Mbps),andhigh-speed(480Mbps)。从设计可以看出,USB3....

码农17年的博客 1万+

用TextKit实现表情,图片,文字混排

用Xcode以前本身自带的coreText,coreImage,实现图文混排,代码量非常大,不容易理解, 而Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的 UILabel,UITextField,UITextView里面就可以使用了。 //传你...

weixin_30882895的博客 145

上万条对话白分析了?智能客服质检避坑指南:90%团队倒在这些误区

当前客服质检普遍存在投入产出失衡的现象,根源在于十大误区:过度关注单点技术指标而忽视端到端解决能力、僵化的话术模板限制AI应变、数据孤岛阻碍知识进化、盲目追求拟人化牺牲效率、传统整改机制跟不上机器速度等。破局需重构质检体系,包括建立决策树质检法、设计复杂问题测试集、构建实时数据闭环、重置AI专属评价指标、实施分钟级热修复机制等。关键在于将质检从被动纠错升级为主动赋能,通过根因分析、场景化反馈、标准化评分和数据驱动优化,打造"问题诊断-改进-验证"

2202_75469062的博客 1544

使用STM32制作智能窗帘控制

以上就是一个简单的基于STM32的智能窗帘控制代码案例。通过感知光线强度和控制步进电机的旋转,我们可以实现智能窗帘的自动开启和关闭。通过定时器的中断来控制步进电机的旋转,从而实现窗帘的开启和关闭动作。最后,我们需要实现主函数和中断函数。在主函数中,我们需要初始化硬件资源,然后进入一个无限循环来不断检测光敏电阻的值,并根据其值来控制窗帘的开启和关闭。接下来,我们需要编写功能函数,用于控制窗帘的开启和关闭。然后,我们需要配置STM32开发环境,并创建一个新的工程。接下来,我们初始化相关的硬件资源。

2401_85258012的博客 280

用TextKit实现表情混排  

http://www.cocoachina.com/bbs/read.php?tid=144272 转载于:https://blog.51cto.com/5828666/1672507

weixin_33766805的博客 113

手把手教你学matlab(8.33)--simulink:DC-DC 双向 Buck-Boost 转换器仿真实例

通过以上步骤,你应该能够建立一个带有 PI 控制器的 DC-DC 双向 Buck-Boost 转换器仿真模型,并观察其性能。Simulink 提供了丰富的工具和模块,可以帮助你进一步优化和调整仿真模型。

MHD0815的博客 960

TextKit实现图文混排

http://blog.sina.com.cn/s/blog_6f1a34260101ptox.html Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。

远远100的专栏 714

在 ABAP SEGW OData 服务里,把 Function Import 参数长度校验真正跑起来

SAP Gateway服务中Function Import参数长度校验的实现机制解析:在SEGW中定义Max Length参数后,需通过模型特性USE_STRICT_FUNCTION_PARAM_CHK(7.52+版本支持)启用严格检查。该特性需在MPC_EXT的DEFINE方法中设置,框架会在URI解析阶段拦截超长参数,避免错误进入业务逻辑。对于低版本系统,建议在应用层手动添加校验。这种模型驱动的方式符合Clean Core原则,能有效提升接口健壮性。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 504

深度学习中常用的几种卷积(下篇):膨胀卷积、可分离卷积(深度可分离、空间可分离)、分组卷积(附Pytorch测试代码)

膨胀卷积也称为扩张卷积或空洞卷积,一般可通过对标准二维卷积的卷积核设置合适的膨胀率(具体地指卷积核元素之间的距离)实现。合理利用膨胀卷积可在不引入其他参数的同时,扩大输出中元素的感受野,提高特征的全局表示能力。下面通过展示几个不同膨胀率下的卷积核大小以及感受野变化情况,可以更直观的理解:图中灰色为输入,蓝色为卷积核,黄色为输出。1)dilation=1:表示卷积核元素之间距离为1,使用0进行大小为dilation-1=0的填充(这是默认情况),因此。

NorthSmile的博客 9021

Textkit

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。 TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的UILabel,UITex

768

地平线旭日X3 安装xfce桌面+VNC查看USB摄像头

VNC是Virtual Network Computing的缩写,它是一种使用RFB(Remote Frame Buffer)协议的用于控制远程电脑的图形桌面分享系统。xfce桌面相比于GNOME桌面更加轻量,占用的资源更少,在没有GPU的设备上也可以运行(旭日的Desktop版本从外观上好像就是xfce桌面,具体没有尝试)。可以使用专用的VNC客户端连接到开发板,例如vnc viewer。板子默认带有openssh-server,因此,WiFi热点连接成功后,就可以使用ssh登录开发板了。

bingdund的博客 515

用TextKit实现图文混排(转载)

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活 了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。 TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的 U...

weixin_30678349的博客 105

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

图文混排(TextKit)

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。    TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的UILabel,UITe

帅帅的专栏 966

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

ios开发text kit_TextKit

一、简介iOS中实现富文本的方式一般有三种:第一种是CoreText,这两天看了唐巧的介绍CoreText的文章,感觉太复杂了,虽然能勉强看懂,但是应用起来有些困难。第二种就是用UIWebView和WKWebView,这个要依赖于前端工程师。第三种就是TextKit了,这种方式实现起来,相对于CoreText简单很多,而且是iOS原生的,不依赖前端开发人员,iOS7以后都是支持的。TextKit是...

weixin_39811386的博客 221

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

用textkit实现图文混排

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。 TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的UILabel,UITex

u010859230的专栏 603

IOS开发笔记(十三)——实现IM的聊天记录,基于UITableView图文混排

中山大学数据科学与计算机学院本科生实验报告 (2019年春季学期) 课程名称 IOS开发 任课老师 郑贵锋 年级 16 专业(方向) 软件工程(计算机应用方向) 学号 16340132 姓名 梁颖霖 电话 13680473185 Email dic0k@qq.com 开始日期 2019/6/9 完成日期 2019/6/12 一、实验题目 IM聊天工具 二、实现内容...

dick的博客 1659

textkit相关做图文混排键盘

无论是富媒体输入还是聊天表情输入,都经常切换系统键盘和自定义输入View。跟Android不一样的是,iOS需要在输入控件如UITextView聚焦才能出现键盘,默认情况下一旦输入控件失焦点有没有切换到其他输入控件的时候,键盘随之消失。尽管,可以通过text属性添加响应内容,但是没有光标的位置提示总是个缺陷。 如腾讯的微信和手Q的iOS版都是没有很好处理光标问题,特别是微信iOS版的表情输入还是

kaka 660
上一篇: 多线程编程1 - NSThread
下一篇: 【ejabberd】安装XMPP服务器ejabberd(Ubuntu 12.04)
focusjava
博客等级 码龄14年 127粉丝 528原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址