UILabel算是iOS里使用最多的控件了吧,下面来说说它特殊的应用需求吧
一、设置字间距、行间距
这是一个比较常见的需求了
写一个UILabel的分类
@interface UILabel (extension)
/**
* 设置字间距
*/
-(void)setColumnSpace:(CGFloat)columnSpace;
/**
* 设置行距
*/
- (void)setRowSpace:(CGFloat)rowSpace;
@end
#import "UILabel+extension.h"
#import
@implementation UILabel (extension)
- (void)setColumnSpace:(CGFloat)columnSpace
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//调整间距
[attributedString addAttribute:(__bridge NSString *)kCTKernAttributeName value:@(columnSpace) range:NSMakeRange(0, [attributedString length])];
self.attributedText = attributedString;
}
- (void)setRowSpace:(CGFloat)rowSpace
{
self.numberOfLines = 0;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//调整行距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = rowSpace;
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
self.attributedText = attributedString;
}
@end
然后再重写UILabel
@interface RDNewLabel : UILabel
@property (nonatomic, assign) float charactSpace; //字间距
@property (nonatomic, assign) float lineSpace; //行间距
+ (CGFloat)textHeight:(NSStr