项目中的要求总是多种多样的.最近公司项目有新的要求.举个例子来说,UIlabel在5s上的字体是16号,正常情况下在6p上面也是16号.但这样看起来在6p上面就显的字体太小.于是需求也应运而生,就是字体在6p上也要有一定的放大,放大比例就是是屏幕宽度的放大比例.
这样也很简单,就是在初始化的时候,只需要让label的font乘以一个比例值就行了,但项目已经成形,之前的label辣么多一个一个改起来也是相当的费劲.
思考之下.做了一个方案.就是创建label的类别,在其中用runtime方法实现setFont方法的交换,在自己的方法中在进行设置比例的设定.这样,之前写过的也就不用改了.
再者,就是计算高度,用frame初始化的Label,左右间距都是数字,这样很容易计算.但,现在项目中估计大多数都是XIB或者用Masonry做的约束吧.这样以来,如果左右间距的约束也有相应比例值的计算.临时用临时计算也是个麻烦事.现在,我在label的类别中实现了这样的计算方法,就是用frame初始化的label有获取高度的方法,包括行间距的设定.用约束设置的label,只需要把label的父视图和内容contentStr传进方法中,也能同样计算出高度.
代码技术含量也不高.但想用的话也不用大家在自己写了.copy过去就OK了.
==========================代码分割线==============================
.h类中的代码
#import@interface UILabel (XbnFontScale)
/**
* @author Zn, 16-07-08 13:07:21
*
* 返回带有自定义行间距高度的label内容高度
*
* @param str 内容
* @param width 基准宽度
* @param lineSpace 自定义的行高
*
* @return
*/
- (CGFloat)returnTheLabelContentHeightWithContentStr:(NSString *)str baseWidth:(CGFloat)width baseCustomLineSpace:(CGFloat)lineSpace;
/**
* @author , 16-07-08 13:07:00
*
* 返回label内容高度
*
* @param str 内容
* @param width 基准宽度
*
* @return
*/
- (CGFloat)returnTheLabelContentHeightWithContentStr:(NSString *)str baseWidth:(CGFloat)width;
/**
* @author , 16-07-08 16:07:32
*
* 如果label不是用frame初始化,而是用约束设置的左右间距,那这个时候用这个方法来返回内容高度
* 左右约束必须都有,不然也没有意义
* @param str 内容
* @param lineSpace 行间距,没有传0
* @param superView label的父视图
*
* @return 内容高度
*/
- (CGFloat)returnTheConstraintsLabelContentHeightWithContentStr:(NSString *)str baseCustomLineSpace:(CGFloat)lineSpace accordingToLabelSuperView:(UIView *)superView;
@end
.m中代码
#import "UILabel+XbnFontScale.h"
#import
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#define wScale W/320
#define hScale H/568
@implementation UILabel (XbnFontScale)
/**
* @author Zn, 16-07-08 09:07:47
*
* 交换setFont 方法,在新方法中进行适配.代码中依然调用.font方法即可
*/
+ (void)load
{
Class class = [self class];
SEL originalSelector = @selector(setFont:);
SEL swizzledSelector = @selector(setFontScale:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)setFontScale:(UIFont *)font {
CGFloat s = font.pointSize*wScale;
UIFont *scaleFont = [UIFont systemFontOfSize:s];
[self setFontScale:scaleFont];
}
/**
* @author Zn, 16-07-08 14:07:45
*
* 返回有自定义行间距的label高度
*
*
*/
- (CGFloat)returnTheLabelContentHeightWithContentStr:(NSString *)str baseWidth:(CGFloat)width baseCustomLineSpace:(CGFloat)lineSpace
{
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.lineSpacing = lineSpace; //行高
paraStyle.hyphenationFactor = 0.0;
paraStyle.firstLineHeadIndent = 0.0;//第一行缩进
paraStyle.paragraphSpacingBefore = 0.0;//段落间距
paraStyle.headIndent = 0.0;//头缩进
paraStyle.tailIndent = 0.0;//尾缩进
//NSKernAttributeName 会影响高度的计算.并且是四舍五入的.0.4就是0. 0.5就是1.具体作用需要再测
NSDictionary *dic = @{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@0.0f};
CGSize s = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return s.height;
}
/**
* @author Zn, 16-07-08 14:07:08
*
* 返回默认行间距的label高度
*
*
*/
- (CGFloat)returnTheLabelContentHeightWithContentStr:(NSString *)str baseWidth:(CGFloat)width
{
CGSize s = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font,NSKernAttributeName:@0.0f} context:nil].size;
return s.height;
}
/**
* @author Zn, 16-07-08 16:07:32
*
* 如果label不是用frame初始化,而是用约束设置的左右间距,那这个时候用这个方法来返回内容高度
* 左右约束必须都有,不然也没有意义
*
*/
- (CGFloat)returnTheConstraintsLabelContentHeightWithContentStr:(NSString *)str baseCustomLineSpace:(CGFloat)lineSpace accordingToLabelSuperView:(UIView *)superView
{
CGFloat height = 0.0;
if (str.length <= 0) {
return height;
}
NSArray *constrainsArr = superView.constraints;//获取约束集
if (constrainsArr.count == 0) {
return height;
}
CGFloat leftSpace = 0.0;
CGFloat rightSpace = 0.0;
for (NSLayoutConstraint *constraint in constrainsArr) {//masonry也是继承自NSLayoutConstraint
/*
举例解释:A B两个视图,B 是A的子视图. [A addSubView: B];
B 设置的top,leading, bottom, trailing四个约束,无论在何种情况下:
top 与 leading 的约束的firstItem都是子视图B,secondItem 是父视图A
bottom 与 trailing 的约束的firstItem都是父视图A, secondItem是子视图B
*/
//这里获取本 label
UILabel *labelFirst = (UILabel *)constraint.firstItem;
UILabel *lableSecond = (UILabel *)constraint.secondItem;
if (labelFirst == self || lableSecond == self) {
if (constraint.firstAttribute == NSLayoutAttributeLeading || constraint.firstAttribute == NSLayoutAttributeLeft) {//获取到label的左侧约束
leftSpace = constraint.constant;
NSLog(@"================%lf",leftSpace);
}
if (constraint.secondAttribute == NSLayoutAttributeTrailing || constraint.secondAttribute == NSLayoutAttributeRight) {//获取到label的右侧约束
rightSpace = -constraint.constant;
NSLog(@"------------------===%lf",rightSpace);
}
}
}
//最终拿到了label的基准宽度,往下计算高度就简单了
CGFloat baseWidth = W - leftSpace - rightSpace;
//调用方法计算高度
if (lineSpace <= 0.0) {
height = [self returnTheLabelContentHeightWithContentStr:str baseWidth:baseWidth];
}else{
height = [self returnTheLabelContentHeightWithContentStr:str baseWidth:baseWidth baseCustomLineSpace:lineSpace];
}
return height;
}
@end
例子调用
UILabel *l = [[UILabel alloc] init];
l.backgroundColor = [UIColor orangeColor];
l.font = [UIFont systemFontOfSize:15];
l.textAlignment = NSTextAlignmentCenter;
l.numberOfLines = 0;
[self.view addSubview:l];
[l mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(50);
make.top.mas_equalTo(180);
make.trailing.mas_equalTo(-30);
}];
NSString *str = @"阿萨德;卡上;当客服;爱迪生;开了房间啊;速度快了解放;阿卡丽束带结发;案例看世界东方;阿卡丽世纪东方;阿卡丽束带结发;阿卡丽束带结发;卡死了都放假;";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
float lineSpace = 5.0;
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
l.attributedText = attributedString;
CGFloat www = W - 80.0;
// CGFloat h1 = [l returnTheLabelContentHeightWithContentStr:str baseWidth:www];
CGFloat h2 = [l returnTheLabelContentHeightWithContentStr:str baseWidth:www baseCustomLineSpace:lineSpace];
NSLog(@"=====%lf",h2);
CGFloat hh = [l returnTheConstraintsLabelContentHeightWithContentStr:str baseCustomLineSpace:lineSpace accordingToLabelSuperView:self.view];
NSLog(@"=======%lf",hh);
测试结果,h2 与 hh 高度一样.
时间仓促,如果有问题或者不对的地方,敬请指正.谢谢