美文网首页
使用富文本OHAttributedLabel

使用富文本OHAttributedLabel

作者: xiaoli飞刀 | 来源:发表于2016-04-15 10:26 被阅读444次

    使用教程:

    请在arc下使用,不要arc与mrc混用造成内存泄露!

    源码地址  http://pan.baidu.com/s/1pjny8bl

    #import "ohattributedlabel.h"

    #import "ohparagraphstyle.h"

    #import "ohtouchesgesturerecognizer.h"

    复制代码

    // 注册字体

    regis地理信息系统 ter_font(bundlefont(@"xindixiaowanzi.ttf"), @"新蒂小丸子体");

    regis地理信息系统 ter_font(bundlefont(@"huakangshaonv.ttf"), @"华康少女字体");

    // 创建富文本string

    nsmutableattributedstring* attrstr = \

    [nsmutableattributedstring attributedstringwithstring:\

    @"还记得吗,窗外那被月光染亮的海洋\

    你还记得吗,是爱让彼此把夜点亮\

    为何后来我们用沉默取代依赖 曾经朗朗星空,渐渐阴霾\

    心碎离开,

    此文来自: 马开东博客  转载请注明出处 网址:  http://www.makaidong.com

    转身回到最初荒凉里等待 为了寂寞,是否找个人填心中空白\

    我们变成了世上,最熟悉的陌生人 今后各自曲折,各自悲哀\

    只怪我们爱得那么汹涌,爱得那么深 于是梦醒了搁浅了沉默了挥手了\

    却回不了神,如果当初在交会时能忍住了 激动的灵魂"];

    // 设置富文本基本属性

    [attrstr setfont:[uifont fontwithname:custom_font(@"华康少女字体", 0) size:12]];

    [attrstr settextcolor:[uicolor whitecolor]];

    [attrstr settextcolor:[uicolor redcolor] range:nsmakerange(9,4)];

    [attrstr settextisunderlined:yes range:nsmakerange(9, 4)];

    [attrstr settextcolor:[uicolor greencolor] range:nsmakerange(125,30)];

    [attrstr setfontname:custom_font(@"新蒂小丸子体", 0) size:12 range:nsmakerange(125,30)];

    // 设置样式

    ohparagraphstyle* paragraphstyle = [ohparagraphstyle defaultparagraphstyle];

    paragraphstyle.textalignment = kcttextalignmentleft;

    paragraphstyle.linebreakmode = kctlinebreakbyword wrapping;

    paragraphstyle.firstlineheadindent = 0.f; // indentation for first line

    paragraphstyle.linespacing = 3.f; // increase space between lines by 3 points

    [attrstr setparagraphstyle:paragraphstyle];

    // 初始化富文本label

    ohattributedlabel *label = \

    [[ohattributedlabel alloc] initwithframe:cgrectmake(0, 0, 300, 200)];

    label.attributedtext = attrstr;

    label.center = self.view.center;

    // 添加进主视图

    [self.view addsubview:label];

    复制代码

    以下是渲染的效果:

    http://images.cnitblog.com/i/607542/201404/240921260765297.png

    复制代码

    // 注册字体

    regis地理信息系统 ter_font(bundlefont(@"xindixiaowanzi.ttf"), @"新蒂小丸子体");

    regis地理信息系统 ter_font(bundlefont(@"huakangshaonv.ttf"), @"华康少女字体");

    // 创建富文本string

    nsmutableattributedstring* attrstr = \

    [nsmutableattributedstring attributedstringwithstring:\

    @"游贤明\

    合抱之木,生于毫末;九层之合,起于垒土;千里之行,始于足下。 "];

    // 设置富文本基本属性

    [attrstr setfont:[uifont fontwithname:custom_font(@"华康少女字体", 0) size:12]];

    [attrstr settextcolor:[uicolor whitecolor]];

    [attrstr settextcolor:[uicolor redcolor] range:nsmakerange(0,3)];

    [attrstr setfontname:custom_font(@"新蒂小丸子体", 0) size:26 range:nsmakerange(0,3)];

    // 设置样式

    ohparagraphstyle* paragraphstyle = [ohparagraphstyle defaultparagraphstyle];

    paragraphstyle.textalignment = kcttextalignmentleft;

    paragraphstyle.linebreakmode = kctlinebreakbyword wrapping;

    paragraphstyle.firstlineheadindent = 0.f; // indentation for first line

    paragraphstyle.linespacing = 3.f; // increase space between lines by 3 points

    [attrstr setparagraphstyle:paragraphstyle];

    // 初始化富文本label

    ohattributedlabel *label = \

    [[ohattributedlabel alloc] initwithframe:cgrectmake(0, 0, 300, 200)];

    label.attributedtext = attrstr;

    label.center = self.view.center;

    // 添加进主视图

    [self.view addsubview:label];

    复制代码

    更加完美的设置:

    复制代码

    // 注册字体

    regis地理信息系统 ter_font(bundlefont(@"xindixiaowanzi.ttf"), @"新蒂小丸子体");

    regis地理信息系统 ter_font(bundlefont(@"huakangshaonv.ttf"), @"华康少女字体");

    // 创建富文本string

    nsmutableattributedstring* attrstr = \

    [nsmutableattributedstring attributedstringwithstring:\

    @"还记得吗,窗外那被

    此文来自: 马开东博客  转载请注明出处 网址:  http://www.makaidong.com

    月光染亮的海洋\n你还记得吗,是爱让彼此把夜点亮\n为何后来我们用沉默取代依赖,曾经朗朗星空,渐渐阴霾\n心碎离开,转身回到最初荒凉里等待\n为了寂寞,是否找个人填心中空白\n我们变成了世上,最熟悉的陌生人\n今后各自曲折,各自悲哀\n只怪我们爱得那么汹涌,爱得那么深\n于是梦醒了搁浅了沉默了挥手了却回不了神\n如果当初在交会时能忍住了,激动的灵魂"];

    // 设置富文本基本属性

    [attrstr setfontname:custom_font(@"华康少女字体", 0) size:12];

    [attrstr settextcolor:[uicolor whitecolor]];

    [attrstr settextcolor:[uicolor redcolor] range:nsmakerange(9,7)];

    // 首字大写以及颜色设置

    [attrstr setfontname:custom_font(@"华康少女字体", 0) size:23 range:nsmakerange(0,1)];

    [attrstr settextcolor:[uicolor yellowcolor] range:nsmakerange(0,1)];

    // 中间字体重新设定

    [attrstr setfontname:custom_font(@"新蒂小丸子体", 0) size:17 range:nsmakerange(92,16)];

    [attrstr settextcolor:[uicolor cyancolor] range:nsmakerange(92,16)];

    // 设置样式

    ohparagraphstyle* paragraphstyle = [ohparagraphstyle defaultparagraphstyle];

    paragraphstyle.textalignment = kcttextalignmentcenter;

    paragraphstyle.paragraphspacing = 3.f;

    paragraphstyle.paragraphspacingbefore = 10.f;

    paragraphstyle.linebreakmode = kctlinebreakbyword wrapping;

    paragraphstyle.firstlineheadindent = 0.f; // indentation for first line

    paragraphstyle.linespacing = 3.f; // increase space between lines by 3 points

    [attrstr setparagraphstyle:paragraphstyle];

    // 初始化富文本label

    ohattributedlabel *label = \

    [[ohattributedlabel alloc] initwithframe:cgrectmake(0, 0, 300, 400)];

    label.attributedtext = attrstr;

    label.center = self.view.center;

    复制代码

    用富文本label来显示文章

    复制代码

    // 注册字体

    regis地理信息系统 ter_font(bundlefont(@"xindixiaowanzi.ttf"), @"新蒂小丸子体");

    regis地理信息系统 ter_font(bundlefont(@"huakangshaonv.ttf"), @"华康少女字体");

    // 创建富文本string

    nsmutableattributedstring* attrstr = \

    [nsmutableattributedstring attributedstringwithstring:\

    @"人啊,再强大,又怎去敌那一抔黄土呢?说到底,这人生也不过就是山一程,水一程。与其把自己锁于跌撞里郁怨寡欢、得失不衡,还不如莞然一笑,迎着阳光,把所有的疼痛与繁复,都一一踏在脚下抛置身后。再以最安然祥和的颜靥,来谢过这一场生。 \n------题记/云微若雨\n是否,在佛堂看僧敲木鱼听梵音钟声,于寺庙拈香诵经濯骨洗心,虔诚脱胎如出家之人,方可让内心,少一份纷扰,多一份恬淡呢?可是,这六界风沙本就是那么的强烈与无常啊,怎去奢求一方安宁?尘世烟火如是冷凛如此淡薄,寄居萧瑟,飘絮染野,人们能做的,想是唯有抚著这颗涩涩的心,捡拾满地残红,浅浅掠过吧。"];

    // 设置富文本基本属性

    [attrstr setfontname:custom_font(@"华康少女字体", 0) size:12];

    [attrstr settextcolor:[uicolor whitecolor]];

    // 设置段落样式

    ohparagraphstyle* paragraphstyle = [ohparagraphstyle defaultparagraphstyle];

    paragraphstyle.textalignment = kcttextalignmentnatural;

    paragraphstyle.paragraphspacing = 12.f;

    paragraphstyle.paragraphspacingbefore = 10.f;

    paragraphstyle.linebreakmode = kctlinebreakbyword wrapping;

    paragraphstyle.firstlineheadindent = 24.f;  // 段落首字缩进

    paragraphstyle.linespacing = 4.f;          // 段落中两行的行间距

    [attrstr setparagraphstyle:paragraphstyle];

    // 初始化富文本label

    ohattributedlabel *label = \

    [[ohattributedlabel alloc] initwithframe:cgrectmake(0, 0, 300, 400)];

    label.attributedtext = attrstr;

    label.center = self.view.center;

    // 添加进主视图

    [self.view addsubview:label];

    var url = window.location.href;document.write("此文链接:"+url+"

    ");document.write("转载请注明出处:"+document.title+"");

    相关文章

      网友评论

          本文标题:使用富文本OHAttributedLabel

          本文链接:https://www.haomeiwen.com/subject/aehnlttx.html