1.初始化控件(文本控件)
UILabel *etlbl = [[UILable alloc]init];
2.設(shè)置基本屬性及用法
- frame:設(shè)置UILable的顯示起點(diǎn)坐標(biāo)(x,y)和寬高(width,height)
etlbl.frame = CGRectMake(<#x起點(diǎn)#>, <#y起點(diǎn)#>, <#寬度#>,<#高度#>);
- text:設(shè)置文本控件的內(nèi)容(即文字顯示的內(nèi)容)
etlbl.text = @"文字顯示內(nèi)容";
- font:文字字體大小及類型(常規(guī),加粗,細(xì)小等)
etlbl.font = [UIFont systemFontOfSize:15];
- textColor:文字顏色
etlbl.textColor = [UIColor greenColor];
- shadowOffSet:陰影的大小
x軸及y軸的偏移量
etlbl.shadowOffset = CGSizeMake(5, 5);
- shadowColor:陰影部分顏色
etlbl.shadowColor = [UIColor yellowColor];
- textAlignment:文字的對其方式(劇中,左對齊,右對齊),系統(tǒng)默認(rèn)的三種方式
etlbl.textAlignment = NSTextAlignmentCenter; //中心對齊(默認(rèn)橫向?qū)R)
etlbl.textAlignment = NSTextAlignmentRight; //文字靠右對齊
etlbl.textAlignment = NSTextAlignmentLeft; //文字靠左對齊
- lineBreakMode
- attributedText:可以自定義某部分顯示大小和顏色
配合 NSMutableAttributedString屬性介紹及使用
/**********屬性方法***********/
//為某一范圍內(nèi)文字設(shè)置多個(gè)屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
//為某一范圍內(nèi)文字添加某個(gè)屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//為某一范圍內(nèi)文字添加多個(gè)屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
//移除某范圍內(nèi)的某個(gè)屬性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
/**********常用屬性keys***********/
NSFontAttributeName //字體
NSParagraphStyleAttributeName //段落格式
NSForegroundColorAttributeName //字體顏色
NSBackgroundColorAttributeName //背景顏色
NSStrikethroughStyleAttributeName//刪除線格式
NSUnderlineStyleAttributeName //下劃線格式
NSStrokeColorAttributeName //刪除線顏色
NSStrokeWidthAttributeName //刪除線寬度
NSShadowAttributeName //陰影
全部NSMutableAttributedString屬性,key and value
//***********keys***********************************Value****************************************//
NSFontAttributeName; //字體,value是UIFont對象
NSParagraphStyleAttributeName; //繪圖的風(fēng)格(居中,換行模式,間距等諸多風(fēng)格),value是NSParagraphStyle對象
NSForegroundColorAttributeName; // 文字顏色,value是UIFont對象
NSBackgroundColorAttributeName; // 背景色,value是UIFont
NSLigatureAttributeName; // 字符連體,value是NSNumber
NSKernAttributeName; // 字符間隔
NSStrikethroughStyleAttributeName; //刪除線,value是NSNumber
NSUnderlineStyleAttributeName; //下劃線,value是NSNumber
NSStrokeColorAttributeName; //描繪邊顏色,value是UIColor
NSStrokeWidthAttributeName; //描邊寬度,value是NSNumber
NSShadowAttributeName; //陰影,value是NSShadow對象
NSTextEffectAttributeName; //文字效果,value是NSString
NSAttachmentAttributeName; //附屬,value是NSTextAttachment 對象
NSLinkAttributeName; //鏈接,value是NSURL or NSString
NSBaselineOffsetAttributeName; //基礎(chǔ)偏移量,value是NSNumber對象
NSUnderlineColorAttributeName; //下劃線顏色,value是UIColor對象
NSStrikethroughColorAttributeName; //刪除線顏色,value是UIColor
NSObliquenessAttributeName; //字體傾斜
NSExpansionAttributeName; //字體扁平化
NSVerticalGlyphFormAttributeName; //垂直或者水平,value是 NSNumber,0表示水平,1垂直
/*****************************************************************************************************/
基本用法如下:
/************************************************某區(qū)域內(nèi)************************************************************/
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"0元"];
[string setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(string.length-1, 1)];
etlbl.attributedText = string;
/************************************************基本用法************************************************************/
NSString *content = @"內(nèi)容太多,需要自適應(yīng)才能解決問題,所以需要寫這個(gè)擴(kuò)展類,內(nèi)容太多,需要自適應(yīng)才能解決問題,所以需要寫這個(gè)擴(kuò)展類";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:content];
//字體大小
[string addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:10]
range:NSMakeRange(0, 1)];
//字體顏色
[string addAttribute:NSForegroundColorAttributeName
value:[UIColor yellowColor]
range:NSMakeRange(1, 1)];
//字體背景顏色
[string addAttribute:NSBackgroundColorAttributeName
value:[UIColor purpleColor]
range:NSMakeRange(2, 1)];
//添加下劃線
[string addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(3, 1)];
//添加下劃線顏色
[string addAttribute:NSUnderlineColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(3, 1)];
UILabel *etlbl3 = [[UILabel alloc]initWithFrame:CGRectMake(100, 400, 200, 30)];
etlbl3.attributedText = string;
[self.view addSubview:etlbl3];
/************************************************基本用法************************************************************/
- userInteractionEnabled:設(shè)置是否可以用戶交互,默認(rèn)NO;一般應(yīng)用于文本控件添加觸摸手勢
- numberOfLines:設(shè)置文本可以顯示的行數(shù),默認(rèn)1行;如果需要自適應(yīng)文本高度需要設(shè)置0行
-
最后需要將控件添加到父視圖上
*補(bǔ)充:富文本中間橫線(效果及代碼如下)
中間橫線.png
#define SystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = [NSString stringWithFormat:@"%.1f", 9999.0];
NSRange strRange1 = {[@"京東價(jià):" length], [str length]};
NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", @"京東價(jià):", str]];
if (SystemVersion >= 10.3) {
[attributeMarket addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:strRange1];
[attributeMarket addAttribute:NSBaselineOffsetAttributeName
value:@(NSUnderlineStyleNone)
range:strRange1];
} else {
[attributeMarket addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:strRange1];
}
[attributeMarket addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:strRange1];
UILabel *etlbl3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 200, CGRectGetWidth(self.view.frame)-40, 30)];
etlbl3.attributedText = attributeMarket;
[self.view addSubview:etlbl3];
}
3.特殊用法
1.自適應(yīng)高度或者寬度
UILable擴(kuò)展類及使用方法:
etlbl.text = @"內(nèi)容太多,需要自適應(yīng)才能解決問題,所以需要寫這個(gè)擴(kuò)展類";
[etlbl setAutoHeigh];
typedef enum : NSUInteger {
NSTextAlignmentVertailTop,
NSTextAlignmentVertailTypeMiddle,//defalut
NSTextAlignmentVertailTypeBottom,
} NSTextAlignmentVertailType;
@interface UILabel (Extern)
/**
* 自動(dòng)設(shè)置高度
*/
- (void)setAutoHeigh;
/**
* 設(shè)置垂直方向?qū)R
*
* @param textAlignment 對齊方式
*/
- (void)setTextAlginVertail:(NSTextAlignmentVertailType)textAlignment;
/**
* 獲取textContent內(nèi)容所需要的尺寸
*
* @return size
*/
- (CGSize)getContentSize;
@end
@implementation UILabel (Extern)
- (void)setAutoHeigh{
NSAssert(self.frame.size.width==0||self.frame.size.height==0?nil:@"", @"請?jiān)O(shè)置UILable的width或者Height");
[self setNumberOfLines:0];
CGSize size = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, size.height);
}
- (void)setTextAlginVertail:(NSTextAlignmentVertailType)textAlignment{
CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
double finalHeight = fontSize.height*self.numberOfLines;
double finalWidth = self.frame.size.width;
CGSize stringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
int newLinesToPad = (finalHeight - stringSize.width) /fontSize.width;
switch (textAlignment) {
case NSTextAlignmentVertailTop:{
for (int i = 0; i<newLinesToPad; i++) {
self.text = [self.text stringByAppendingString:@"\n "];
}
}
break;
case NSTextAlignmentVertailTypeMiddle:{
} break;
case NSTextAlignmentVertailTypeBottom:{
for(int i=0; i<newLinesToPad; i++){
self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
break;
default:
break;
}
}
- (CGSize)getContentSize{
return [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.font}
context:nil].size;
}
2.獲取文本內(nèi)容所需要的CGSize
NSString的擴(kuò)展類及用法
NSString *content = @"內(nèi)容太多,需要自適應(yīng)才能解決問題,所以需要寫這個(gè)擴(kuò)展類,內(nèi)容太多,需要自適應(yīng)才能解決問題,所以需要寫這個(gè)擴(kuò)展類";
UIFont *font = [UIFont systemFontOfSize:15];
CGFloat maxHeigth = 200;
CGFloat maxWidht = 200;
CGSize size1 = [NSString sizeTextContentMaxHeight:maxHeigth font:font withContent:content];
CGSize size2 = [NSString sizeTextContentMaxWidth:maxWidht font:font withContent:content];
CGFloat width = [NSString widthTextContentMaxHeight:maxHeigth font:font withContent:content];
CGFloat height= [NSString heigtTextContentMaxWidth:maxWidht font:font withContent:content];
@interface NSString (Extern)
/**
* 計(jì)算文本內(nèi)容的高度(輸入寬度,獲取高度)
*
* @param width 最大寬度
* @param font 字體大小
* @param content 文本內(nèi)容
*
* @return height
*/
+ (CGFloat)heigtTextContentMaxWidth:(CGFloat)maxWidth font:(UIFont*)font withContent:(NSString*)content;
/**
* 計(jì)算文本內(nèi)容的寬度(輸入高度,獲取寬度)
*
* @param width 最大高度
* @param font 字體大小
* @param content 文本內(nèi)容
*
* @return width
*/
+ (CGFloat)widthTextContentMaxHeight:(CGFloat)maxHeight font:(UIFont*)font withContent:(NSString*)content;
/**
* 計(jì)算文本內(nèi)容的尺寸(輸入寬度,獲取尺寸)
*
* @param width 最大寬度
* @param font 字體大小
* @param content 文本內(nèi)容
*
* @return size
*/
+ (CGSize)sizeTextContentMaxWidth:(CGFloat)maxWidth font:(UIFont*)font withContent:(NSString*)content;
/**
* 計(jì)算文本內(nèi)容的尺寸(輸入高度,獲取尺寸)
*
* @param height 最大高度
* @param font 字體大小
* @param content 文本內(nèi)容
*
* @return size
*/
+ (CGSize)sizeTextContentMaxHeight:(CGFloat)maxheight font:(UIFont*)font withContent:(NSString*)content;
@end
@implementation NSString (Extern)
+ (CGFloat)heigtTextContentMaxWidth:(CGFloat)maxWidth font:(UIFont*)font withContent:(NSString*)content{
return [self sizeTextContentMaxWidth:maxWidth font:font withContent:content].height;
}
+ (CGFloat)widthTextContentMaxHeight:(CGFloat)maxHeight font:(UIFont*)font withContent:(NSString*)content{
return [self sizeTextContentMaxHeight:maxHeight font:font withContent:content].width;
}
+ (CGSize)sizeTextContentMaxWidth:(CGFloat)maxWidth font:(UIFont*)font withContent:(NSString*)content{
return [content boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
}
+ (CGSize)sizeTextContentMaxHeight:(CGFloat)maxheight font:(UIFont*)font withContent:(NSString*)content{
return [content boundingRectWithSize:CGSizeMake(MAXFLOAT,maxheight)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
}
@end
4.UILabel的居中上中下方向?qū)R基類
使用方法如下:
TJLabel *etlbl = [[TJLabel alloc]init];
etlbl.frame = CGRectMake(0, 0, 200, 40);
etlbl.text = @"文字內(nèi)容";
[etlbl setVerticalAlignment:NSTextVerticalAlignmentBottom];
[self.view addSubview:etlbl];
.h文件
/**
* UILabel設(shè)置居上對齊,居中對齊,居下對齊
*/
typedef NS_ENUM(NSInteger, NSTextVerticalAlignment) {
NSTextVerticalAlignmentTop = 0,
NSTextVerticalAlignmentMiddle,
NSTextVerticalAlignmentBottom
};
@interface TJLabel : UILabel
{
@private
NSTextVerticalAlignment _verticalAlignment;
}
@property (nonatomic) NSTextVerticalAlignment verticalAlignment;
@end
.m文件
@implementation TJLabel
@synthesize verticalAlignment = verticalAlignment_;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.verticalAlignment = NSTextVerticalAlignmentMiddle;
}
return self;
}
- (void)setVerticalAlignment:(NSTextVerticalAlignment)verticalAlignment {
verticalAlignment_ = verticalAlignment;
[self setNeedsDisplay];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
switch (self.verticalAlignment) {
case NSTextVerticalAlignmentTop:
textRect.origin.y = bounds.origin.y;
break;
case NSTextVerticalAlignmentBottom:
textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
break;
case NSTextVerticalAlignmentMiddle:
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
