星空网 > 软件开发 > 操作系统

ParagraphString

ParagraphString - 段落样式的简易处理

ParagraphStringimages/loading.gif' data-original="http://images2015.cnblogs.com/blog/607542/201611/607542-20161116171400310-492530075.gif" >

 

效果

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(0, self.string.length);        NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string];        self.font      ? [richString addAttribute:NSFontAttributeName      value:self.font range:range]      : 0;    self.textColor   ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range]   : 0;    self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : 0;        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 = 0;    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(0, self.string.length);  NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string];    self.font      ? [richString addAttribute:NSFontAttributeName      value:self.font range:range]      : 0;  self.textColor   ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range]   : 0;  self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : 0;    UILabel *label    = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];  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

关键词:string

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Prime Day易踩的坑 ,稍不注意,亏损百万!:https://www.ikjzd.com/articles/1676767201479827457
暴击!轻小商品计划取消,亚马逊再调FBA配送费:https://www.ikjzd.com/articles/1676786115950178306
Shoptop上新「礼品卡」功能,轻松提高复购率:https://www.ikjzd.com/articles/1676828239265558530
HaiMa海玛国际|Colorkey如何用“一只唇釉”靠海外社媒营销引爆市场?!:https://www.ikjzd.com/articles/1676837723761307649
如何用好亚马逊品牌分析中的搜索词表现 数据?:https://www.ikjzd.com/articles/1676839541320392705
亚马逊于8月29日取消轻小商品计划,卖家进退两难:https://www.ikjzd.com/articles/1676856853012357121
​TikTok发生了什么?美区增长首次停滞,准入管理力度加大:https://www.kjdsnews.com/a/1836407.html
2024年如何找到在TikTok上发帖的最佳时间:https://www.kjdsnews.com/a/1836408.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流