UILabel的各種屬性與方法的使用(轉)
#import "LabelTestViewController.h"
@implementation LabelTestViewController
/*
Accessing the Text Attributes
text property
font property
textColor property
textAlignment property
lineBreakMode property
enabled property
Sizing the Label’s Text
adjustsFontSizeToFitWidth property
baselineAdjustment property
minimumFontSize property 無例
numberOfLines property
Managing Highlight Values
highlightedTextColor property
highlighted property
Drawing a Shadow
shadowColor property
shadowOffset property
Drawing and Positioning Overrides
– textRectForBounds:limitedToNumberOfLines: 無例
– drawTextInRect: 無例
Setting and Getting Attributes
userInteractionEnabled property
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
//設定顯示文字
label1.text = @"label1";
label2.text = @"label2";
label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
label4.text = @"label4--label4--label4--label4--";
label5.text = @"label5--label5--label5--label5--label5--label5--";
label6.text = @"label6";
label7.text = @"label7";
//設定字型:粗體,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//設定文字顔色
label1.textColor = [UIColor orangeColor];
label2.textColor = [UIColor purpleColor];
//設定文字位置
label1.textAlignment = UITextAlignmentRight;
label2.textAlignment = UITextAlignmentCenter;
//設定字型大小适應label寬度
label4.adjustsFontSizeToFitWidth = YES;
//設定label的行數
label5.numberOfLines = 2;
UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
//設定高亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//設定陰影
label7.shadowColor = [UIColor redColor];
label7.shadowOffset = CGSizeMake(1.0,1.0);
//設定是否能與使用者進行互動
label7.userInteractionEnabled = YES;
//設定label中的文字是否可變,預設值是YES
label3.enabled = NO;
//設定文字過長時的顯示格式
label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中間
// typedef enum {
// UILineBreakModeWordWrap = 0,
// UILineBreakModeCharacterWrap,
// UILineBreakModeClip,//截去多餘部分
// UILineBreakModeHeadTruncation,//截去頭部
// UILineBreakModeTailTruncation,//截去尾部
// UILineBreakModeMiddleTruncation,//截去中間
// } UILineBreakMode;
//如果adjustsFontSizeToFitWidth屬性設定為YES,這個屬性就來控制文本基線的行為
label4.baselineAdjustment = UIBaselineAdjustmentNone;
// UIBaselineAdjustmentAlignBaselines,
// UIBaselineAdjustmentAlignCenters,
// UIBaselineAdjustmentNone,
// } UIBaselineAdjustment;
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
[self.view addSubview:label5];
[self.view addSubview:label6];
[self.view addSubview:label7];
[label1 release];
[label2 release];
[label3 release];
[label4 release];
[label5 release];
[label6 release];
[label7 release];
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc {
[super dealloc];
@end
UIFont字型設定
一、建立任意樣式字型
label.font = [UIFont fontWithName:@"fontName" size:17];
label.font = [label.font fontWithSize:17];
二、建立指定大小的系統預設字型(預設:Helvetica)
label.font = [UIFont systemFontOfSize:17];
label.font = [UIFont boldSystemFontOfSize:17]; // 指定大小粗體
label.font = [UIFont italicSystemFontOfSize:17]; // 指定大小斜體
三、擷取可用的字型名數組
NSArray *fontFamilies = [UIFont familyNames]; // 傳回所有可用的fontFamily
NSArray *fontNames = [UIFont fontNamesForFamilyName:@"fongFamilyName"]; // 傳回指定fontFamily下的所有fontName
四、擷取指定字型的familyName/fontName
NSString *familyName = [label.font familyName];
NSString *fontName = [label.font fontName];
五、擷取系統标準字型大小
CGFloat labelFontSize = [UIFont labelFontSize]; // Returns the standard font size used for labels.
CGFloat buttonFontSize = [UIFont buttonFontSize]; // Returns the standard font size used for buttons.
CGFloat smallSystemFontSize = [UIFont smallSystemFontSize]; // Returns the size of the standard small system font.
CGFloat systemFontSize = [UIFont systemFontSize]; // Returns the size of the standard system font.
附字型樣式表:
Thonburi
-Thonburi-Bold
-Thonburi
Snell Roundhand
-SnellRoundhand-Bold
-SnellRoundhand-Black
-SnellRoundhand
Academy Engraved LET
-AcademyEngravedLetPlain
Avenir
-Avenir-LightOblique
-Avenir-MediumOblique
-Avenir-Medium
-Avenir-HeavyOblique
-Avenir-BlackOblique
-Avenir-Oblique
-Avenir-Book
-Avenir-Roman
-Avenir-BookOblique
-Avenir-Light
-Avenir-Heavy
-Avenir-Black
Marker Felt
-MarkerFelt-Wide
-MarkerFelt-Thin
Geeza Pro
-GeezaPro-Bold
-GeezaPro
Arial Rounded MT Bold
-ArialRoundedMTBold
Trebuchet MS
-TrebuchetMS
-TrebuchetMS-Bold
-TrebuchetMS-Italic
-Trebuchet-BoldItalic
Arial
-Arial-BoldMT
-ArialMT
-Arial-ItalicMT
-Arial-BoldItalicMT
Marion
-Marion-Regular
-Marion-Bold
-Marion-Italic
Gurmukhi MN
-GurmukhiMN
-GurmukhiMN-Bold
Malayalam Sangam MN
-MalayalamSangamMN-Bold
-MalayalamSangamMN
Bradley Hand
-BradleyHandITCTT-Bold
Kannada Sangam MN
-KannadaSangamMN
-KannadaSangamMN-Bold
Bodoni 72 Oldstyle
-BodoniSvtyTwoOSITCTT-Book
-BodoniSvtyTwoOSITCTT-Bold
-BodoniSvtyTwoOSITCTT-BookIt
Cochin
-Cochin
-Cochin-BoldItalic
-Cochin-Italic
-Cochin-Bold
Sinhala Sangam MN
-SinhalaSangamMN
-SinhalaSangamMN-Bold
Hiragino Kaku Gothic ProN
-HiraKakuProN-W6
-HiraKakuProN-W3
Papyrus
-Papyrus-Condensed
-Papyrus
Verdana
-Verdana
-Verdana-Bold
-Verdana-BoldItalic
-Verdana-Italic
Zapf Dingbats
-ZapfDingbatsITC
Avenir Next Condensed
-AvenirNextCondensed-HeavyItalic
-AvenirNextCondensed-DemiBold
-AvenirNextCondensed-Italic
-AvenirNextCondensed-Heavy
-AvenirNextCondensed-DemiBoldItalic
-AvenirNextCondensed-Medium
-AvenirNextCondensed-BoldItalic
-AvenirNextCondensed-Bold
-AvenirNextCondensed-UltraLightItalic
-AvenirNextCondensed-UltraLight
-AvenirNextCondensed-MediumItalic
-AvenirNextCondensed-Regular
Courier
-Courier-Bold
-Courier
-Courier-BoldOblique
-Courier-Oblique
Hoefler Text
-HoeflerText-Black
-HoeflerText-Italic
-HoeflerText-Regular
-HoeflerText-BlackItalic
Helvetica
-Helvetica-LightOblique
-Helvetica
-Helvetica-Oblique
-Helvetica-BoldOblique
-Helvetica-Bold
-Helvetica-Light
Euphemia UCAS
-EuphemiaUCAS-Bold
-EuphemiaUCAS
-EuphemiaUCAS-Italic
Hiragino Mincho ProN
-HiraMinProN-W3
-HiraMinProN-W6
Bodoni Ornaments
-BodoniOrnamentsITCTT
Apple Color Emoji
-AppleColorEmoji
Optima
-Optima-ExtraBlack
-Optima-Italic
-Optima-Regular
-Optima-BoldItalic
-Optima-Bold
Gujarati Sangam MN
-GujaratiSangamMN
-GujaratiSangamMN-Bold
Devanagari Sangam MN
-DevanagariSangamMN
-DevanagariSangamMN-Bold
Times New Roman
-TimesNewRomanPS-ItalicMT
-TimesNewRomanPS-BoldMT
-TimesNewRomanPSMT
-TimesNewRomanPS-BoldItalicMT
Kailasa
-Kailasa
-Kailasa-Bold
Telugu Sangam MN
-TeluguSangamMN-Bold
-TeluguSangamMN
Heiti SC
-STHeitiSC-Medium
-STHeitiSC-Light
Apple SD Gothic Neo
-AppleSDGothicNeo-Bold
-AppleSDGothicNeo-Medium
Futura
-Futura-Medium
-Futura-CondensedExtraBold
-Futura-CondensedMedium
-Futura-MediumItalic
Bodoni 72
-BodoniSvtyTwoITCTT-BookIta
-BodoniSvtyTwoITCTT-Book
-BodoniSvtyTwoITCTT-Bold
Baskerville
-Baskerville-SemiBoldItalic
-Baskerville-Bold
-Baskerville-Italic
-Baskerville-BoldItalic
-Baskerville-SemiBold
-Baskerville
Chalkboard SE
-ChalkboardSE-Regular
-ChalkboardSE-Bold
-ChalkboardSE-Light
Heiti TC
-STHeitiTC-Medium
-STHeitiTC-Light
Copperplate
-Copperplate
-Copperplate-Light
-Copperplate-Bold
Party LET
-PartyLetPlain
American Typewriter
-AmericanTypewriter-CondensedLight
-AmericanTypewriter-Light
-AmericanTypewriter-Bold
-AmericanTypewriter
-AmericanTypewriter-CondensedBold
-AmericanTypewriter-Condensed
Symbol
-Symbol
Avenir Next
-AvenirNext-Heavy
-AvenirNext-DemiBoldItalic
-AvenirNext-UltraLightItalic
-AvenirNext-HeavyItalic
-AvenirNext-MediumItalic
-AvenirNext-UltraLight
-AvenirNext-BoldItalic
-AvenirNext-DemiBold
-AvenirNext-Bold
-AvenirNext-Regular
-AvenirNext-Medium
-AvenirNext-Italic
Noteworthy
-Noteworthy-Light
-Noteworthy-Bold
Bangla Sangam MN
-BanglaSangamMN-Bold
-BanglaSangamMN
Zapfino
-Zapfino
Tamil Sangam MN
-TamilSangamMN
-TamilSangamMN-Bold
Chalkduster
-Chalkduster
Arial Hebrew
-ArialHebrew
-ArialHebrew-Bold
Georgia
-Georgia-Italic
-Georgia-BoldItalic
-Georgia-Bold
-Georgia
Helvetica Neue
-HelveticaNeue-Bold
-HelveticaNeue-CondensedBlack
-HelveticaNeue-Medium
-HelveticaNeue
-HelveticaNeue-Light
-HelveticaNeue-CondensedBold
-HelveticaNeue-LightItalic
-HelveticaNeue-UltraLightItalic
-HelveticaNeue-UltraLight
-HelveticaNeue-BoldItalic
-HelveticaNeue-Italic
Gill Sans
-GillSans-LightItalic
-GillSans-BoldItalic
-GillSans-Italic
-GillSans
-GillSans-Bold
-GillSans-Light
Palatino
-Palatino-Roman
-Palatino-Bold
-Palatino-BoldItalic
-Palatino-Italic
Courier New
-CourierNewPS-BoldMT
-CourierNewPSMT
-CourierNewPS-BoldItalicMT
-CourierNewPS-ItalicMT
Oriya Sangam MN
-OriyaSangamMN-Bold
-OriyaSangamMN
Didot
-Didot-Italic
-Didot
-Didot-Bold
Bodoni 72 Smallcaps
-BodoniSvtyTwoSCITCTT-Book
文本大小自适應
文本大小自适應需要調用NSString類的執行個體方法計算出文本的size,然後根據這個size來設定UILabel的frame來實作。計算size的方法有:
(1) - sizeWithFont:
- (CGSize)countTextSize:(NSString *)text
{
/*
1、換行方式預設取NSLineBreakByWordWrapping;
2、求出的size是單行顯示時的高度和寬度.
*/
UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
CGSize size = [text sizeWithFont:font];
return size;
}
(2) - sizeWithFont: forWidth: lineBreakMode:
1、如果指定寬度小于字元串寬度,則寬度傳回0;
CGSize size = [text sizeWithFont:font forWidth:400.0f lineBreakMode:NSLineBreakByWordWrapping];
(3) - sizeWithFont: constrainedToSize:
字元串用指定字型在指定區域進行單行顯示時,需要的高度和寬度;
一般的用法是,指定區域的高度固定而寬度用MAXFLOAT,則傳回值包含對應的寬度;
如果指定區域的寬度不夠,則寬度傳回0;高度不夠則沒影響;
核心:單行顯示,指定區域的寬度要夠大,擷取寬度.
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 100.0f)];
(4) - sizeWithFont: constrainedToSize: lineBreakMode: (最常用)
字元串在指定區域内按照指定的字型顯示時,需要的高度和寬度(寬度在字元串隻有一行時有用)
一般用法:指定區域的寬度而高度用MAXFLOAT,則傳回值包含對應的高度
如果指定區域的寬度指定,而字元串要顯示的區域的高度超過了指定區域的高度,則高度傳回0
核心:多行顯示,指定寬度,擷取高度
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(320.f, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
(5) - sizeWithFont: minFontSize: actualFontSize: forWidth: lineBreakMode:
雖然指定了換行方式,在實際計算時也會換行,但傳回的結果隻是第一行的高度很寬度;
指定了應該顯示的字型,最小的字型,實際的字型,在實際計算中,如果寬度不夠,則盡量縮小字元串的字型直至能夠一行全部顯示,如果縮到最小還不能完全顯示字元串,則進行截斷,傳回截斷後的字元串的高度和寬度;
字型實際的大小,存放在 actualFontSize裡.
CGFloat f = 0.0f;
CGSize size = [text sizeWithFont:font minFontSize:10.0f actualFontSize:&f forWidth:100.0f lineBreakMode:NSLineBreakByWordWrapping];
本文轉自夏雪冬日部落格園部落格,原文連結:http://www.cnblogs.com/heyonggang/p/3508754.html,如需轉載請自行聯系原作者