2:动态计算文字大小
/**
*
* @param font 字体大小
* @param size 限制的size
*/
-(CGSize)newSizeWithFont:(UIFont *)font andSize:(CGSize)size{
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
}
例如: CGSize lableSize=[lable.text newSizeWithFont:[UIFont systemFontOfSize:13] andSize:CGSizeMake(320, 60)];
3:label文字显示位置,靠顶部,居中,靠底部
实现代码如下:
在.h文件里:
#import <UIKit/UIKit.h>
ypedef enum
{
VerticalAlignmentTop = 0, // default
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interface Mylabel : UILabel{//自定义UILabel
@private
VerticalAlignment _verticalAlignment;
}
@property (nonatomic) VerticalAlignment verticalAlignment;
@end
在.m文件里:
#import "Mylabel.h"
@implementation Mylabel
@synthesize verticalAlignment = verticalAlignment_;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.verticalAlignment = VerticalAlignmentMiddle;
}
return self;
}
- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
verticalAlignment_ = verticalAlignment;
[self setNeedsDisplay];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
switch (self.verticalAlignment) {
case VerticalAlignmentTop:
textRect.origin.y = bounds.origin.y;
break;
case VerticalAlignmentBottom:
textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
break;
case VerticalAlignmentMiddle:
default:
textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
}
return textRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:actualRect];
}
@end
4:UILabel上加删除线
首先生成一个继承与UILabel的类SFPStrikeThroughAndUnderLineLabel
一,在.h文件里
@interface SFPStrikeThroughAndUnderLineLabel : UILabel
{
BOOL _isWithStrikeThrough;
}
@property (nonatomic, assign) BOOL isWithStrikeThrough; //控制是否显示删除线
@property (nonatomic, assign) CGFloat strikeThroughLineWidth;//设置删除线的宽度
@property (nonatomic, retain) NSArray *strikeThroughRGBAlphaArray;//设置删除线的颜色,数组里面的元素分别是RGB以及alpha值也就是说该数组有四个元素
@end
二,在.m文件里
@implementation SFPStrikeThroughAndUnderLineLabel
@synthesize isWithStrikeThrough = _isWithStrikeThrough;
@synthesize strikeThroughRGBAlphaArray = _strikeThroughRGBAlphaArray;
@synthesize strikeThroughLineWidth = _strikeThroughLineWidth;
{
self = [super initWithFrame:frame];
if (self) {
returnself;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.isWithStrikeThrough)//显示删除线
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat color[4] = {[[self.strikeThroughRGBAlphaArrayobjectAtIndex:0] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:1] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:2] floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:3] floatValue]};
CGContextSetStrokeColor(c, color);
CGContextSetLineWidth(c, self.strikeThroughLineWidth);
CGContextBeginPath(c);
CGFloat halfWayUp = (self.bounds.size.height - self.bounds.origin.y) / 2.0;
CGContextMoveToPoint(c, self.bounds.origin.x, halfWayUp );
CGContextAddLineToPoint(c, self.bounds.origin.x + self.bounds.size.width, halfWayUp);
CGContextStrokePath(c);
}
[super drawRect:rect];
}
三,使用:添加SFPStrikeThroughAndUnderLineLabel.h文件
[self.view addSubview:oneLabel];
注意:这是给UILabel整个控件加删除线(一个label只会有一条删除线),而不是给label的text加删除线
5:如何在Label中显示图片
#import "ViewController.h"
@interface ViewController ()
//成员变量 在storyboard里拖得一个label。
@property (weak, nonatomic) IBOutletUILabel *imageLabel;
@end
@implementation ViewController
- (void)viewDidLoad{
[superviewDidLoad];
//推荐一种用Label系统的属性来做。
UIImage *image=[UIImageimageNamed:@"shareIcon_4"];
NSTextAttachment *textAttch=[[NSTextAttachmentalloc]init];
textAttch.image=image;
NSAttributedString *str=[NSAttributedStringattributedStringWithAttachment:textAttch];
self.imageLabel.attributedText=str;
}
@end
6: 设置行间距
#import <UIKit/UIKit.h>
@interface UILabel (Utils)
//设置行间距
- (void)setLineSpacing:(CGFloat)space;
@end
#import "UILabel+Utils.h"
@implementation UILabel (Utils)
/**
* 计算行间距
*
* @param space 位置
*/
- (void)setLineSpacing:(CGFloat)space
{
if (self.text == nil)
{
return;
}
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = space;
NSRange range = NSMakeRange(0, [[NSString alloc]initWithString:self.text].length);
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
self.attributedText = attributedString;
}
@end