UILabel:
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 20)];
——>Label字体居上显示(方法一:在字符串末尾用回车补齐)
// 对应字号的字体一行显示所占宽高
CGSize fontSize = [Label.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
// 对应字号的内容实际所占范围
CGSize stringSize = [Label.text boundingRectWithSize:CGSizeMake(Label.frame.size.width, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;
// 剩余空行
NSInteger line = (Label.frame.size.height - stringSize.height) / fontSize.height;
// 用回车补齐
for (int i = 0; i < line; i++) {
Label.text = [Label.text stringByAppendingString:@"\n "];
}
Label.numberOfLines=0;
——>Label字体居上或居下显示(方法二:重写一个继承UILabel的类)
//在.h中(该类是继承于UILabel的)
#import <UIKit/UIKit.h>
typedef enum VerticalAlignment {
VerticalAlignmentTop,
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interface JXLabel : UILabel
{
@private
VerticalAlignment verticalAlignment_;
}
@property (nonatomic, assign) VerticalAlignment verticalAlignment;
@end
//在.m中
#import "JXLabel.h"
@implementation JXLabel
@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:
// Fall through.
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
//使用方法:
//使用该类初始化Label(如:JXLabel * Label=[[JXLabel alloc]init];)
//只要加上下面一行代码即可实现文字居上显示:
Label.verticalAlignment = VerticalAlignmentTop;//文字居上显示
Label.verticalAlignment = VerticalAlignmentBottom;//文字居下显示
——>字体加阴影模糊效果
NSShadow *shadow=[[NSShadow alloc]init];
shadow.shadowBlurRadius=4;//模糊度
shadow.shadowColor=[UIColor blackColor];//阴影颜色
shadow.shadowOffset=CGSizeMake(0, 0);//阴影偏移x(正值)向左偏y(正值)向下偏
//绘制文本
NSDictionary *dict=@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSShadowAttributeName:shadow,NSVerticalGlyphFormAttributeName:@(0)};
Label.attributedText = [[NSAttributedString alloc]initWithString:Label.text attributes:dict];
//设置文本字体
UIFont *kaiFont=[UIFont fontWithName:@"STHeitiSC-Light" size:20];
[Label setFont:kaiFont];
——>调整字体行间距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:Label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:7];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [Label.text length])];
Label.attributedText = attributedString;
[Label sizeToFit];