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. Windows Azure Active Directory (2) Windows Azure AD基础

    <Windows Azure Platform 系列文章目录> Windows Azure AD (WAAD)是Windows Azure提供的一个REST风格的服务,为您的云服务提供了身 ...

  2. Swift泛型Protocol对比C#泛型Interface

    本篇纯属抬杠之作,之前我们提到了Swift的泛型Protocol使用associatedtype关键字,而不是使用<Type>语法的泛型参数.这其中有什么好处呢? 我就这个问题搜索了一些回 ...

  3. 7 款免费的 Metro UI 模板

    #1 – Free Metro Ui Style template by Asif Aleem 很棒的蓝色调 Metro UI 管理模板 #2: Metro-Bootstrap by TalksLab ...

  4. Linux 小工具学习之(1)——Wget十例[翻译]

    在本博文中我们将学习一下wget这个工具.wget可以使用HTTP.HTTPS.和FTP等常见网络协议来从万维网(WWW)获取文件(从它的名字来源也可以看出 World Wide Web Get -& ...

  5. Mac下github项目检出与提交

    项目检出 如果你的git还没有代码仓库,可以用过git的代码仓库页面新建一个你的仓库 创建git上的仓库后,我们还需要建立本地的仓库,所以打开Mac终端,建立本地仓库文件夹(这里我用HelloC),然 ...

  6. 在_Layout模版中使用@Styles.Render()没有效果

    刚才有测试一个功能,就是在_Layout母版中使用了@Styles.Render()时行Render样式文件,所有在此母版下的视图均没有应用到样式,没有效果.是什么原因? 经查证资料,原来Insus. ...

  7. IOS系统概述与层次

    一.概述 IOS是apple公司为其自己的移动设备(iPhone,iPod touch,iPad)而开发的操作系统,IOS许多的技术是基于苹果的Mac OSX桌面系统的,如果你开发过苹果的mac系统应 ...

  8. SQLServer中游标是如何处理数据的?

    游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许 ...

  9. 关于Bugzilla WebService接口

    参考:http://www.bugzilla.org/docs/3.2/en/html/api/Bugzilla/WebService.html http://www.bugzilla.org/doc ...

  10. springmvc(2)Controller源码简单解析

    前面简单的分析了一下DispatcherServlet,接下来分析一下Controller,在web的MVC中,Controller就是其中的C,启动的一些页面逻辑处理,页面映射的功能: 首先看看超类 ...