2018年4月12日
1.目前已经支持 文本里面含有换行符\n,在自定义文本间距。
2017年3月24日
一.实现自定义高度的文本行间距
1.效果:
2.实现
第一步定义扩展函数
// UILabel+Category.h
#import
@interface UILabel (HSPXAdd)
/*一般控制UILabel中文字不同风格显示的步骤
1>先设置段落的风格NSMutableParagraphStyle/NSParagraphStyle;
2>再设置NSAttributedString对象;
3>NSAttributedString对象赋值给UILabel的attributedText;*/
- (CGSize)multipleLinesSizeWithLineSpacing:(CGFloat)lineSpacing andViewWidth:(CGFloat)viewWidth;
@end
#import "UILabel+Category.h"
@implementation UILabel (HSPXAdd)
+ (instancetype)customLablWithFrame:(CGRect)frame andTitle:(NSString *)text andFont:(UIFont *)font
{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.font = font;
return label;
}
- (CGSize)multipleLinesSizeWithLineSpacing:(CGFloat)lineSpacing andViewWidth:(CGFloat)viewWidth
{
self.numberOfLines = 0;
NSString *text = self.text;
if ([text containsString:@"\n"]) {
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:lineSpacing];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
NSArray *textArr = [text componentsSeparatedByString:@"\n"];
CGFloat allRealHeight = 0;
for(int i = 0; i < textArr.count; i++){
NSString *text = textArr[i];
if (text.length == 0) {
text = @"oneLine";
}
CGFloat oneRowHeight = [@"oneLine" sizeWithAttributes:@{NSFontAttributeName:self.font}].height;
CGSize textSize = [text boundingRectWithSize:CGSizeMake(viewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
CGFloat rows = ceil(textSize.height / oneRowHeight);
CGFloat realHeight;
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing;
allRealHeight += realHeight;
}
allRealHeight += (textArr.count - 1) * lineSpacing;
[self setAttributedText:attributedString1];
return CGSizeMake(viewWidth, allRealHeight);
}else{
CGFloat oneRowHeight = [text sizeWithAttributes:@{NSFontAttributeName:self.font}].height;
CGSize textSize = [text boundingRectWithSize:CGSizeMake(viewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:lineSpacing];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
CGFloat rows = textSize.height / oneRowHeight;
CGFloat realHeight = oneRowHeight;
if (rows > 1) {
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing;
}
[self setAttributedText:attributedString1];
return CGSizeMake(textSize.width, realHeight);
}
}
@end
第二步,定义文本自适应行间距接口,其实和上面接口类似 【因为即使cell高度的时候,我们还没有UILabel,所有要用文本接口】
// UIKitTool.h
#import
@interface UIKitTool : NSObject
//计算带有行间距的文本大小
+ (CGSize)multipleLinesSizeWithLineSpacing:(CGFloat)lineSpacing andViewWidth:(CGFloat)viewWidth andText:(NSString*)text andFont:(UIFont*)font;
@end
// UIKitTool.m
#import "UIKitTool.h"
@implementation UIKitTool
+ (CGSize)multipleLinesSizeWithLineSpacing:(CGFloat)lineSpacing andViewWidth:(CGFloat)viewWidth andText:(NSString*)text andFont:(UIFont*)font
{
if([text containsString:@"\n"]){
NSArray *textArr = [text componentsSeparatedByString:@"\n"];
CGFloat allRealHeight = 0;
for(int i = 0; i < textArr.count; i++){
NSString *text = textArr[i];
if (text.length == 0) {
text = @"oneLine";
}
CGFloat oneRowHeight = [@"oneLine" sizeWithAttributes:@{NSFontAttributeName:font}].height;
CGSize textSize = [text boundingRectWithSize:CGSizeMake(viewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:lineSpacing];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
CGFloat rows = ceil(textSize.height / oneRowHeight);
CGFloat realHeight;
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing; //
allRealHeight += realHeight;
}
allRealHeight += (textArr.count - 1) * lineSpacing;
return CGSizeMake(viewWidth, allRealHeight);
}else{
CGFloat oneRowHeight = [text sizeWithAttributes:@{NSFontAttributeName:font}].height;
CGSize textSize = [text boundingRectWithSize:CGSizeMake(viewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:lineSpacing];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
CGFloat rows = textSize.height / oneRowHeight;
CGFloat realHeight = oneRowHeight;
if (rows > 1) {
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing;
}
// [self setAttributedText:attributedString1];
return CGSizeMake(textSize.width, realHeight);
}
}
@end
第三步,如下使用即可
// NewFlowerTableViewCell.h
#define sendWord_lineSpace 4
#import
@interface NewFlowerTableViewCell : UITableViewCell
- (void)config:(id)model;
+ (CGFloat)cellHeightWithModel:(id)model;
@end
// NewFlowerTableViewCell.m
@implementation NewFlowerTableViewCell
- (void)layoutAllView:(id )model
{
NSString *sendWord = @"寄语:该护士很体贴,该护士很体贴,该护士很体贴,该护士很体贴,";
//////
//寄语
if ([sendWord length] > 0) {
yPos = tempLabel.bottom + sendWord_uMargin;
tempLabel = _sendWordL;
CGSize size = [tempLabel multipleLinesSizeWithLineSpacing:sendWord_lineSpace andViewWidth:width];
height = size.height;
frame = CGRectMake(xPos, yPos, width, height);
_sendWordL.frame = frame;
}
/////
}
+ (CGFloat)cellHeightWithModel:(id)model
{
NSString *sendWord = @"寄语:该护士很体贴,该护士很体贴,该护士很体贴,该护士很体贴,";
////////
//寄语:
if ([sendWord length] > 0) {
NSString *sendStr = [NSString stringWithFormat:@"寄语:%@",sendWord];
CGSize size = [UIKitTool multipleLinesSizeWithLineSpacing:sendWord_lineSpace andViewWidth:width andText:sendStr andFont:[self getSendWordFont]];
height += sendWord_uMargin + size.height;
}
/////////
return height;
}
@end
2017年3月5日
一.输出可以变色的文本
1.效果:
2.实现:
//注意str格式 @"题目 %ld / %lu"
- (NSMutableAttributedString *)getChangeColorText:(NSInteger)showNum andTotalNum:(NSUInteger)totalNum
{
NSString *part1 = @"题目";
NSString *part2 = [NSString stringWithFormat:@" %ld",showNum];
NSString *part3 = [NSString stringWithFormat:@" / %lu",totalNum];
NSInteger len1 = part1.length;
NSInteger len2 = part2.length;
NSInteger len3 = part3.length;
NSString *str = [NSString stringWithFormat:@"%@%@%@",part1,part2,part3];
NSMutableAttributedString *attributedStr = nil;
NSInteger length = len1;
NSInteger index = 0;
attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
[attributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#666666"]
range:NSMakeRange(index, length)];
index = index + length;
length = len2;
[attributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#3DC92B"]
range:NSMakeRange(index, length)];
index = index + length;
length = len3;
[attributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#4D94E2"]
range:NSMakeRange(index, length)];
return attributedStr;
}
或者
{
// _summaryL.text = [NSString stringWithFormat:@"全部%@题,正确%@题,错误%@题",model.total,model.successCount,model.failCount];
//变色效果
NSString *str = [NSString stringWithFormat:@"全部%@题,正确%@题,错误%@题",model.total,model.successCount,model.failCount];
NSMutableAttributedString *attributedStr = nil;
attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
NSString *part1 = [NSString stringWithFormat:@"全部%@题,正确",model.total];
NSString *part2 = [NSString stringWithFormat:@"%@",model.successCount];
NSInteger index = part1.length;
NSInteger length = part2.length;
[attributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#3DC92B"]
range:NSMakeRange(index, length)];
part1 = [NSString stringWithFormat:@"全部%@题,正确%@题,错误",model.total,model.successCount];
part2 = [NSString stringWithFormat:@"%@",model.failCount];
index = part1.length;
length = part2.length;
[attributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#FF513C"]
range:NSMakeRange(index, length)];
_summaryL.attributedText = attributedStr;
}
3.使用
_topRightLabel.attributedText = [self getChangeColorText:showNum andTotalNum:totalNum];
UILabel *tmpLabel = _topRightLabel;
CGSize size = [tmpLabel.text sizeWithAttributes: @{NSFontAttributeName:tmpLabel.font}];
tmpLabel.frame = CGRectMake(0, 0, size.width, size.height);
2017年2月5日
1.文本的frame设置
1.1根据文本字体大小设置 文本的frame
CGSize size = [tmpLabel.text sizeWithAttributes: @{NSFontAttributeName:tmpLabel.font}];
tmpLabel.frame = CGRectMake(xPos, yPos, viewWidth, size.height);
1.2也可以先让文本自适应
_title = [[UILabel alloc] initWithFrame:CGRectZero];
_title.numberOfLines = 1;
_title.textColor = msg_title_color;
_title.font = msg_title_font;
_title.text = [attachment title];
[_title sizeToFit];
[self addSubview:_title];
2016年3月2日
1.文本居中
_summaryL.textAlignment = NSTextAlignmentCenter;
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。