·常用屬性和方法有:
1、建立
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
2、text //設定和讀取文字內容,預設為nil
label.text = @”文字資訊”; //設定內容
NSLog(@”%@”, label.text); //讀取內容
3、textColor //設定文字顏色,預設為黑色
lable.textColor = [UIColor redColor];
4、font //設定字型大小,預設17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
字型的方法
//還有⼀一種從外部匯入字型的方法。
5、textAlignment //設定標籤文字對齊方式。
label.textAlignment = NSTextAlignmentCenter; //還有
NSTextAlignmentLeft、 NSTextAlignmentRight.
6、numberOfLines //標籤最多顯示行數,如果為0則表示多行。
label.numberOfLines = 2;
7、enabled //只是決定了Label的繪製方式,將它設定
為NO將會使文字變暗,表示它沒有啟用,這時向它設定顏色值是無效的。
label.enable = NO;
8、highlighted //是否高亮顯示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮
顯示時的文字顏色
9、ShadowColor //設定陰影顏色
[label setShadowColor:[UIColor blackColor]];
10、ShadowOffset //設定陰影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
11、baselineAdjustment //如果adjustsFontSizeToFitWidth屬性設
置為YES,這個屬性就來控制文字基線的行為。
label.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines = 0,預設,文字最上端與中線對齊。
UIBaselineAdjustmentAlignCenters, 文字中線與label中線對齊。
UIBaselineAdjustmentNone, 文字最低端與label中線對齊。
12、Autoshrink //是否自動收縮
Fixed Font Size 預設,如果Label寬度小於文字長度時時,文字大小不自動縮放
minimumScaleFactor 設定最小收縮比例,如果Label寬度小於文字長度時,文字
進行收縮,收縮超過比例後,停止收縮。
minimumFontSize 設定最小收縮字號,如果Label寬度小於文字長度時,文字字號
減小,低於設定字號後,不再減小。//6.0以後不再使用了。
label.minimumScaleFactor = 0.5;
13、adjustsLetterSpacingToFitWidth //改變字母之間的間距來適應Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;
14、 lineBreakMode //設定文字過長時的顯示格式
label.lineBreakMode = NSLineBreakByCharWrapping;以字元為顯示單位顯
示,後面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪下與文字寬度相同的內
容長度,後半部分被刪除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字
以……方式省略,顯示尾部文字內容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內容
以……方式省略,顯示頭尾的文字內容。
label.lineBreakMode = NSLineBreakByTruncatingTail;結尾部分的內容
以……方式省略,顯示頭的文字內容。
label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯
示,後面部分省略不顯示。
15、 adjustsFontSizeToFitWidth //設定字型大小適應label寬度
label.adjustsFontSizeToFitWidth = YES;
16、attributedText:設定標籤屬性文字。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:text];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor lightGrayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:NSMakeRange(11,
10)];
label.attributedText = textLabelStr;
17、豎排文字顯示每個文字加一個換行符,這是最方便和簡單的實現方式。
label.text = @"請
豎
直
方
向
排
列";
label.numberOfLines = [label.text length];
18、計算UIlabel 隨字型多行後的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //計算20行後的Label的Frame
NSLog(@"%f",heightLabel.size.height);
19、UILabel根據字數多少自動實現適應高度
UILabel *msgLabel = [[UILabel alloc]
initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordWrap;
msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @"獲取到的deviceToken,我們可以通過webservice服務提
交給.net應用程式,這裡我簡單處理,直接列印出來,拷貝到.net應用環境中使
用。";
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];
20、漸變字型Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:@"btn.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
21、Label新增邊框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;
在iOS中預設的UILabel中的文字在豎直方向上只能居中對齊,博主參考國外網站,從UILabel繼承了一個新類,實現了居上對齊,居中對齊,居下對齊。具體如下:
#import <UIKit/UIKit.h>
typedef enum
{
VerticalAlignmentTop = 0, // default
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interface myUILabel : UILabel
{
@private
VerticalAlignment _verticalAlignment;
}
@property (nonatomic) VerticalAlignment verticalAlignment;
@end
//
// myUILabel.m
//
//
// Created by yexiaozi_007 on 3/4/13.
// Copyright (c) 2013 yexiaozi_007. All rights reserved.
//
#import "myUILabel.h"
@implementation myUILabel
@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
在使用時:
lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明圖片作為label的背景色
lbl_mylabel.backgroundColor = color;
lbl_mylabel.textAlignment = UITextAlignmentLeft;
lbl_mylabel.textColor = UIColor.whiteColor;
lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
lbl_mylabel.numberOfLines = 0;
[lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
[self addSubview:lbl_mylabel];
//學習內容
/*
1.控制元件 UIView UILabel UITextField UITextView UIButton
2.字型、大小、單位、顏色
*/
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
label.text = @"Label Text Content, This is a text label things attribute";//預設為空
label.font = [UIFont systemFontOfSize:17];//預設使用系統的17
label.textColor = [UIColor orangeColor];//預設使用文字黑色
label.shadowColor = [UIColor lightGrayColor];//預設沒有陰影
label.shadowOffset = CGSizeMake(1,0);//預設是一個向上的陰影(0,-1)
label.textAlignment = NSTextAlignmentCenter;//預設是左對齊
label.lineBreakMode = NSLineBreakByTruncatingTail;//段落樣式,預設是最後截斷尾巴,用...代替
//富文字的基本資料型別,屬性字串。以此為基礎,如果這個設定了相應的屬性,則會忽略上面設定的屬性,預設為空
NSString *string = label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
//設定字型
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//設定所有的字型
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//設定Text這四個字母的字型為粗體
//設定傾斜,需要匯入coreText
UIFont *italicFont = GetVariationOfFontWithTrait(baseFont,
kCTFontTraitItalic);
[attrString addAttribute:NSFontAttributeName value:italicFont
range:[string rangeOfString:@"Label"]];
// 設定顏色
UIColor *color = [UIColor redColor];
[attrString addAttribute:NSForegroundColorAttributeName
value:color
range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]];
//可以對這些屬性設定值
//字型名稱有以下:
// label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24];
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]];
label.numberOfLines = 2;
NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;//增加行高
style.headIndent = 10;//頭部縮排,相當於左padding
style.tailIndent = -10;//相當於右padding
style.lineHeightMultiple = 1.5;//行間距是多少倍
style.alignment = NSTextAlignmentLeft;//對齊方式
style.firstLineHeadIndent = 20;//首行頭縮排
style.paragraphSpacing = 10;//段落後面的間距
style.paragraphSpacingBefore = 20;//段落之前的間距
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
[attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];//字元間距 2pt
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//設定文字描邊顏色,需要和NSStrokeWidthAttributeName設定描邊寬度,這樣就能使文字空心
[attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//空心字,文字邊框描述
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];//下劃線
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];//厚的下劃線
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];//刪除線
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];//刪除線藍色
label.attributedText = attrString;
label.highlightedTextColor = [UIColor redColor];//設定文字高亮顯示顏色,與highlighted一起使用。
label.highlighted = NO; //高亮狀態是否開啟
label.enabled = YES;//設定文字內容是否可變
label.userInteractionEnabled = YES;//設定標籤是否忽略或移除使用者互動。預設為NO
label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth屬性設定為YES,這個屬性就來控制文字基線的行為。
// UIBaselineAdjustmentAlignBaselines=0,預設,文字最上端與中線對齊。
// UIBaselineAdjustmentAlignCenters, 文字中線與label中線對齊。
// UIBaselineAdjustmentNone, 文字最低端與label中線對齊。;
[self.view addSubview:label];
/*
lineSpacing; 來增加行距
paragraphSpacing;
alignment; 對齊
firstLineHeadIndent; 段落開始的縮排畫素
headIndent; 可調整全部文字的縮排距離,可當作左邊 padding 使用
tailIndent; 可調整文字尾端的縮排距離。需要注意的是,這裡指定的值可以當作文字顯示的寬、而也可當作右邊 padding 使用,依據輸入的正負值而定:
lineBreakMode;
minimumLineHeight;
maximumLineHeight; 而針對不同的字型與字號,我們可以透過指定最大與最小行距(maximumLineHeight 與 minimumLineHeight)來避免過高或過窄的狀況發生。
baseWritingDirection;
lineHeightMultiple; 想要調整行距,可以透過搭配使用 lineHeightMultiple 更改行距倍數
paragraphSpacingBefore; 而若是文章內容有分段落的話,也可以透過指定段落結尾距離(paragraphSpacing)以及段落開頭距離
//獲取斜體
UIFont * GetVariationOfFontWithTrait(UIFont *baseFont,
CTFontSymbolicTraits trait) {
CGFloat fontSize = [baseFont pointSize];
CFStringRef
baseFontName = (__bridge CFStringRef)[baseFont fontName];
CTFontRef baseCTFont = CTFontCreateWithName(baseFontName,
fontSize, NULL);
CTFontRef ctFont =
CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL,
trait, trait);
NSString *variantFontName =
CFBridgingRelease(CTFontCopyName(ctFont,
kCTFontPostScriptNameKey));
UIFont *variantFont = [UIFont fontWithName:variantFontName
size:fontSize];
CFRelease(ctFont);
CFRelease(baseCTFont);
return variantFont;
};