ParagraphString - 段落样式的简易处理

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 中的 ParagraphString

//
// ParagraphString.h
// RichString
//
// Created by YouXianMing on 2016/11/11.
// Copyright © 2016年 TechCode. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BaseParagraphStyle.h" @interface ParagraphString : NSObject /**
The input string.
*/
@property (nonatomic, strong) NSString *string; /**
Set the string's font, default is nil.
*/
@property (nonatomic, strong) UIFont *font; /**
Set the string's textColor, default is nil.
*/
@property (nonatomic, strong) UIColor *textColor; /**
Set the paragraph style, default is nil.
*/
@property (nonatomic, strong) BaseParagraphStyle *paragraphStyle; /**
Make the config (Font, textColor, paragraphStyle) effective.
*/
- (void)makeConfigEffective; /**
The attributedString, before you get this, you should set property and run makeConfigEffective first.
*/
@property (nonatomic, strong, readonly) NSMutableAttributedString *attributedString; /**
The string's height with the fixed width. @param width The specified width.
@return The string's height.
*/
- (CGFloat)heightWithFixedWidth:(CGFloat)width; /**
The string's height with the fixed width. @param lines The number of lines.
@param width The specified width.
@return The string's height.
*/
- (CGFloat)numberOfLines:(NSInteger)lines fixedWidth:(CGFloat)width; /**
ParagraphString's constructor. @param string The string.
@param font The font.
@param color The color.
@param style The paragraph style.
@return The ParagraphString's instance.
*/
+ (instancetype)paragraphStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color
paragraphStyle:(BaseParagraphStyle *)style; @end
//
// ParagraphString.m
// RichString
//
// Created by YouXianMing on 2016/11/11.
// Copyright © 2016年 TechCode. All rights reserved.
// #import "ParagraphString.h" @interface ParagraphString () @property (nonatomic, strong) NSMutableAttributedString *attributedString; @end @implementation ParagraphString - (void)makeConfigEffective { if (self.string) { NSRange range = NSMakeRange(, self.string.length); NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : ;
self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : ;
self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : ; self.attributedString = richString; } else { self.attributedString = nil;
}
} + (instancetype)paragraphStringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color
paragraphStyle:(BaseParagraphStyle *)style { ParagraphString *paragraphString = [[[self class] alloc] init];
paragraphString.string = string;
paragraphString.font = font;
paragraphString.textColor = color;
paragraphString.paragraphStyle = style;
[paragraphString makeConfigEffective]; return paragraphString;
} - (CGFloat)heightWithFixedWidth:(CGFloat)width { CGFloat height = ; if (self.attributedString) { CGRect rect = [self.attributedString boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil]; height = rect.size.height;
} return height;
} - (CGFloat)numberOfLines:(NSInteger)lines fixedWidth:(CGFloat)width { NSRange range = NSMakeRange(, self.string.length);
NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : ;
self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : ;
self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : ; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , width, )];
label.numberOfLines = lines;
label.attributedText = richString;
[label sizeToFit]; return label.frame.size.height;
} @end
//
// BaseParagraphStyle.h
// RichString
//
// Created by YouXianMing on 2016/11/11.
// Copyright © 2016年 TechCode. All rights reserved.
// #import <UIKit/UIKit.h> @interface BaseParagraphStyle : NSMutableParagraphStyle @end
//
// BaseParagraphStyle.m
// RichString
//
// Created by YouXianMing on 2016/11/11.
// Copyright © 2016年 TechCode. All rights reserved.
// #import "BaseParagraphStyle.h" @implementation BaseParagraphStyle @end

ParagraphString - 段落样式的简易处理的更多相关文章

  1. CSS3初学篇章_4(边框样式/段落样式)

    边框样式 1.边框线语法:border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inse ...

  2. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  3. bootstrap学习笔记<二>(标题,段落样式)

    标题.样式:class="h1"~class="h6" bootstrap中重新定义了h1~h6标签,具体差别如下: 在bootstrap中其他任何标签使用cl ...

  4. HTML标题 段落 样式

    HTML 标题 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <h1> 定义最大的标题.<h6> 定义最小的标题. 注释:默 ...

  5. iOS-文本段落样式NSMutableParagraphStyle与NSParagraphStyle的使用和一些富文本处理属性

    开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...

  6. CSS 文章段落样式

    #adiv p { text-align: left; text-indent: 2em; line-height:25px; font-family:微软雅黑; font-size:medium; ...

  7. css段落样式

    字间距 letter-spacing 首行缩进 text-indent

  8. CSS常用样式(二)

    一.边框样式 1.border:复合属性.设置对象边框的特性. 取值: border-width: 设置或检索对象边框宽度. border-style: 设置或检索对象边框样式. border-col ...

  9. CSS第四天总结 更多的属性 圆角 边框图片 段落属性 颜色渐变 盒子阴影

    圆角边框: border-radius    一个值时表示四个角用的圆半径,4个值时分别是左上角.右上角.左下角.右下角,单位可以是px和百分比,百分比是半径相对于边框长度的比例 在CSS3中我们终于 ...

随机推荐

  1. 使用log4j配置不同文件输出不同内容

    敲代码中很不注意写日志,虽然明白很重要.今天碰到记录日志,需要根据内容分别输出到不同的文件. 参考几篇文章: 感觉最详细:http://blog.csdn.net/azheng270/article/ ...

  2. 基于HTML5 WebGL实现3D飞机叶轮旋转

    在上一篇<基于HT for Web矢量实现2D叶轮旋转>中讲述了叶轮旋转在2D拓扑上的应用,今天我们就来讲讲叶轮旋转在3D上的应用. 在3D拓扑上可以创建各种各样的图元,在HT for W ...

  3. 语义化HTML:p、h1-6、q、blockquote、hr、address、code、pre、var、cite、dfn和samp

    一.元素语义 p标签 W3C草案: The p element represents a paragraph.W3C specification 语义化的 <p>元素 表示:文章中的段落. ...

  4. Moon.Orm 5.0(MQL版)分页功能的设计(求指教,邀请您的加入)

    一.分页的分类及分析 1)分页的前置条件: 查询的目标条件.第几页.总页数(本质上由查询条件决定).每页条数.请求地址.按照什么字段怎样排序 2)目标结果: 数据列表,List<T>返回 ...

  5. Python性能提升小技巧

    第一部分 1-使用内建函数: 你可以用Python写出高效的代码,但很难击败内建函数. 经查证. 他们非常快速 2-使用 join() 连接字符串. 你可以使用 + 来连接字符串. 但由于string ...

  6. 微信小程序官方文档错误整理

    大致看了一遍微信小程序文档,发现有几处微小的错误,但瑕不掩瑜.记录下,以后发现了还会继续在此添加.如果有记录不对的,请及时指出错误. 1.视图层->WXSS->尺寸单位 明显错误,应该为 ...

  7. Web API应用支持HTTPS的经验总结

    在我前面介绍的WebAPI文章里面,介绍了WebAPI的架构设计方面的内容,其中提出了现在流行的WebAPI优先的路线,这种也是我们开发多应用(APP.微信.微网站.商城.以及Winform等方面的整 ...

  8. 三星S4,呼转的来电,在来电界面上不显示的解决方案

    因为工作关系,我有2个号码是呼叫转移到我现在的手机上的,如果有呼叫转移的来电的话,在通话界面里面没有提示该来电是呼叫转移来的.之前我用的诺基亚的,可以看到呼叫转移的来电,在号码的左边有个拐弯的箭头,提 ...

  9. 介绍开源的.net通信框架NetworkComms框架 源码分析(十六 ) ConnectionStatic

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  10. C++: read SQL server data using System::Data::SqlClient

    stdafx.h: // stdafx.h : include file for standard system include files, // or project specific inclu ...