UILabel的各种属性与方法的使用(转)

  1. #import "LabelTestViewController.h"
  2. @implementation LabelTestViewController
  3. /*
  4. Accessing the Text Attributes
  5. text  property
  6. font  property
  7. textColor  property
  8. textAlignment  property
  9. lineBreakMode  property
  10. enabled  property
  11. Sizing the Label’s Text
  12. adjustsFontSizeToFitWidth  property
  13. baselineAdjustment  property
  14. minimumFontSize  property   无例
  15. numberOfLines  property
  16. Managing Highlight Values
  17. highlightedTextColor  property
  18. highlighted  property
  19. Drawing a Shadow
  20. shadowColor  property
  21. shadowOffset  property
  22. Drawing and Positioning Overrides
  23. – textRectForBounds:limitedToNumberOfLines: 无例
  24. – drawTextInRect:  无例
  25. Setting and Getting Attributes
  26. userInteractionEnabled  property
  27. */
  28. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  29. - (void)viewDidLoad {
  30. UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
  31. UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
  32. UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
  33. UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
  34. UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
  35. UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
  36. UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
  37. //设置显示文字
  38. label1.text = @"label1";
  39. label2.text = @"label2";
  40. label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
  41. label4.text = @"label4--label4--label4--label4--";
  42. label5.text = @"label5--label5--label5--label5--label5--label5--";
  43. label6.text = @"label6";
  44. label7.text = @"label7";
  45. //设置字体:粗体,正常的是 SystemFontOfSize
  46. label1.font = [UIFont boldSystemFontOfSize:20];
  47. //设置文字颜色
  48. label1.textColor = [UIColor orangeColor];
  49. label2.textColor = [UIColor purpleColor];
  50. //设置文字位置
  51. label1.textAlignment = UITextAlignmentRight;
  52. label2.textAlignment = UITextAlignmentCenter;
  53. //设置字体大小适应label宽度
  54. label4.adjustsFontSizeToFitWidth = YES;
  55. //设置label的行数
  56. label5.numberOfLines = 2;
  57. UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
  58. //设置高亮
  59. label6.highlighted = YES;
  60. label6.highlightedTextColor = [UIColor orangeColor];
  61. //设置阴影
  62. label7.shadowColor = [UIColor redColor];
  63. label7.shadowOffset = CGSizeMake(1.0,1.0);
  64. //设置是否能与用户进行交互
  65. label7.userInteractionEnabled = YES;
  66. //设置label中的文字是否可变,默认值是YES
  67. label3.enabled = NO;
  68. //设置文字过长时的显示格式
  69. label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
  70. //  typedef enum {
  71. //      UILineBreakModeWordWrap = 0,
  72. //      UILineBreakModeCharacterWrap,
  73. //      UILineBreakModeClip,//截去多余部分
  74. //      UILineBreakModeHeadTruncation,//截去头部
  75. //      UILineBreakModeTailTruncation,//截去尾部
  76. //      UILineBreakModeMiddleTruncation,//截去中间
  77. //  } UILineBreakMode;
  78. //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
  79. label4.baselineAdjustment = UIBaselineAdjustmentNone;
  80. //  typedef enum {
  81. //      UIBaselineAdjustmentAlignBaselines,
  82. //      UIBaselineAdjustmentAlignCenters,
  83. //      UIBaselineAdjustmentNone,
  84. //  } UIBaselineAdjustment;
  85. [self.view addSubview:label1];
  86. [self.view addSubview:label2];
  87. [self.view addSubview:label3];
  88. [self.view addSubview:label4];
  89. [self.view addSubview:label5];
  90. [self.view addSubview:label6];
  91. [self.view addSubview:label7];
  92. [label1 release];
  93. [label2 release];
  94. [label3 release];
  95. [label4 release];
  96. [label5 release];
  97. [label6 release];
  98. [label7 release];
  99. [super viewDidLoad];
  100. }
  101. /*
  102. // Override to allow orientations other than the default portrait orientation.
  103. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  104. // Return YES for supported orientations
  105. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  106. }
  107. */
  108. - (void)didReceiveMemoryWarning {
  109. // Releases the view if it doesn't have a superview.
  110. [super didReceiveMemoryWarning];
  111. // Release any cached data, images, etc that aren't in use.
  112. }
  113. - (void)viewDidUnload {
  114. // Release any retained subviews of the main view.
  115. // e.g. self.myOutlet = nil;
  116. }
  117. - (void)dealloc {
  118. [super dealloc];
  119. }
  120. @end

UIFont字体设置

一、创建任意样式字体

  1. label.font = [UIFont fontWithName:@"fontName" size:17];
  2. label.font = [label.font fontWithSize:17];

二、创建指定大小的系统默认字体(默认:Helvetica)

  1. label.font = [UIFont systemFontOfSize:17];
  2. label.font = [UIFont boldSystemFontOfSize:17];  // 指定大小粗体
  3. label.font = [UIFont italicSystemFontOfSize:17];  // 指定大小斜体

三、获取可用的字体名数组

  1. NSArray *fontFamilies = [UIFont familyNames];                           // 返回所有可用的fontFamily
  2. NSArray *fontNames = [UIFont fontNamesForFamilyName:@"fongFamilyName"]; // 返回指定fontFamily下的所有fontName

四、获取指定字体的familyName/fontName

  1. NSString *familyName = [label.font familyName];
  2. NSString *fontName = [label.font fontName];

五、获取系统标准字体大小

  1. CGFloat labelFontSize = [UIFont labelFontSize];    // Returns the standard font size used for labels.
  2. CGFloat buttonFontSize = [UIFont buttonFontSize];  // Returns the standard font size used for buttons.
  3. CGFloat smallSystemFontSize = [UIFont smallSystemFontSize]; // Returns the size of the standard small system font.
  4. 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:

  1. - (CGSize)countTextSize:(NSString *)text
  2. {
  3. /*
  4. 1、换行方式默认取NSLineBreakByWordWrapping;
  5. 2、求出的size是单行显示时的高度和宽度.
  6. */
  7. UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
  8. CGSize size = [text sizeWithFont:font];
  9. return  size;
  10. }

(2)  - sizeWithFont: forWidth: lineBreakMode:

  1. - (CGSize)countTextSize:(NSString *)text
  2. {
  3. /*
  4. 1、如果指定宽度小于字符串宽度,则宽度返回0;
  5. 2、求出的size是单行显示时的高度和宽度.
  6. */
  7. UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
  8. CGSize size = [text sizeWithFont:font forWidth:400.0f lineBreakMode:NSLineBreakByWordWrapping];
  9. return  size;
  10. }

(3)  - sizeWithFont: constrainedToSize:

  1. - (CGSize)countTextSize:(NSString *)text
  2. {
  3. /*
  4. 字符串用指定字体在指定区域进行单行显示时,需要的高度和宽度;
  5. 一般的用法是,指定区域的高度固定而宽度用MAXFLOAT,则返回值包含对应的宽度;
  6. 如果指定区域的宽度不够,则宽度返回0;高度不够则没影响;
  7. 核心:单行显示,指定区域的宽度要够大,获取宽度.
  8. */
  9. UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
  10. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 100.0f)];
  11. return  size;
  12. }

(4)  - sizeWithFont: constrainedToSize: lineBreakMode:  (最常用)

  1. - (CGSize)countTextSize:(NSString *)text
  2. {
  3. /*
  4. 字符串在指定区域内按照指定的字体显示时,需要的高度和宽度(宽度在字符串只有一行时有用)
  5. 一般用法:指定区域的宽度而高度用MAXFLOAT,则返回值包含对应的高度
  6. 如果指定区域的宽度指定,而字符串要显示的区域的高度超过了指定区域的高度,则高度返回0
  7. 核心:多行显示,指定宽度,获取高度
  8. */
  9. UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
  10. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(320.f, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
  11. return  size;
  12. }

(5)  - sizeWithFont: minFontSize: actualFontSize: forWidth: lineBreakMode:

  1. - (CGSize)countTextSize:(NSString *)text
  2. {
  3. /*
  4. 虽然指定了换行方式,在实际计算时也会换行,但返回的结果只是第一行的高度很宽度;
  5. 指定了应该显示的字体,最小的字体,实际的字体,在实际计算中,如果宽度不够,则尽量缩小字符串的字体直至能够一行全部显示,如果缩到最小还不能完全显示字符串,则进行截断,返回截断后的字符串的高度和宽度;
  6. 字体实际的大小,存放在 actualFontSize里.
  7. */
  8. UIFont *font = [UIFont fontWithName:@"Arial" size:20.0f];
  9. CGFloat f = 0.0f;
  10. CGSize size = [text sizeWithFont:font minFontSize:10.0f actualFontSize:&f forWidth:100.0f lineBreakMode:NSLineBreakByWordWrapping];
  11. return  size;
  12. }

参考资料:http://blog.csdn.net/mamong/article/details/8542404

 

UILabel使用技巧的更多相关文章

  1. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  2. 【读书笔记】iOS-开发技巧-UILabel内容模糊的原因

    在非Retina的iPad mini的屏幕上,一个UILabel的frame的origin值如果有小数位数(例如,0.5),就会造成显示模糊.所以最好用整数值的origin坐标. 参考资料: < ...

  3. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  4. IOS UILabel的一些使用小技巧

    1. 你在iOS6的需要NSLineBreakByWordWrapping 为了您的代码试试这个: NSString *string = @"bla"; CGSize s = [s ...

  5. iOS:小技巧(不断更新)

    记录下一些不常用技巧,以防忘记,复制用. 1.获取当前的View在Window的frame: UIWindow * window=[[[UIApplication sharedApplication] ...

  6. iOS-调试技巧

    目录 前言逼优鸡知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global BreakPoint) 条件断点(Condational Breakpoints)打印的艺术 NSLog 开启僵尸对象 ...

  7. iOS 保持界面流畅的技巧 (转载)

    这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如何构建流畅的交互. Index 演示项目 屏幕显示图像的原理 卡顿产生 ...

  8. 如何让iOS 保持界面流畅?这些技巧你知道吗

    如何让iOS 保持界面流畅?这些技巧你知道吗   作者:ibireme这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如 ...

  9. iOS各种调试技巧豪华套餐

    转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...

随机推荐

  1. vc++ mfc 里保存缩放的bmp图片 不失真

    void CSaveView::OnFileSave() { BITMAP info;//原始图片 m_bitmap.GetBitmap(&info); CDC DC1; DC1.Create ...

  2. Retrofit2文件上传下载及其进度显示

    序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...

  3. python命令行添加Tab键自动补全

    1.编写一个tab的自动补全脚本,名为tab.py #!/usr/bin/python # python tab complete import sys import readline import ...

  4. axure复用-自定义组件,母版(模板)

    组件(控件)是用于设计线框图的用户界面元素.在组件(控件)面板中包含有常用的控件库,如按钮.图片.文本框等.从组件面板中拖动一个控件到线框图区域中,就可以添加一个组件.组件可以从一个线框图中被拷贝(C ...

  5. SQL SERVER 生成ORACLE建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...

  6. oracle11g导入dmp文件(根据用户)

    已知:用户名.密码.dmp文件 .(指即将导入dmp文件的用户名和密码) 需求:将该dmp文件导入本地oracle11g中. 步骤: 1.将该dmp文件拷贝到G:\oracle11g\admin\or ...

  7. jsPlumb 学习笔记

    介绍 使用svg完成画图,四个概念: anchor: endpoint在的位置,可通过name访问 endpoint:connection的一端节点,通过addPoint makeSource, co ...

  8. C#利用Windows API 实现关机、注销、重启等操作

    using System; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; nam ...

  9. [Hibernate] - Annotations - One To Many

    Hibernate使用Annotation的一对多: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8&q ...

  10. jquery导航动画

    经常在网上看到的,鼠标在导航上移动时,导航底部的横条会自动移动到鼠标悬浮的导航项上. 效果如下图: 利用jquery的 animate 函数,很好实现.代码很简单! 代码如下: <!DOCTYP ...