ios基础知识iOS 开发

iOS-UILabel

2016-06-10  本文已影响456人  我是谁重要吗

继承关系:UIView : UIResponder : NSObject
///UILabel 显示的文本只读,无法编辑,可以根据文字个数自动换行;
///UITextField 可编辑本文,但是无法换行,只能一行显示;当点击键盘上的return时,会触发事件。
////UITextView 可编辑文本,提供换行功能。

1、声明 初始化 布局

//创建uilabel   
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];

2、背景色

//设置背景色    
   label1.backgroundColor = [UIColor grayColor];
//是否能与用户交互    
   label1.userInteractionEnabled = YES;
//设置tag    
   label1.tag = 91;

3、文本 字体 字间距 行间距 段间距

//设置标签文本    
    label1.text = @"Hello world!";    
//设置标签文本字体和字体大小    
    label1.font = [UIFont fontWithName:@"Arial" size:30];    
        
//字体、字号
//系统字体
    label.font = [UIFont systemFontOfSize:30.0];
//加粗
    label.font = [UIFont boldSystemFontOfSize:30.0];
//斜体
    label.font = [UIFont italicSystemFontOfSize:30.0];
//拿到所有字体
    NSArray* fonts = [UIFont familyNames];
//通过字体名字设置字体
    label.font = [UIFont fontWithName:[fonts objectAtIndex:5] size:30.0];
//文本最多行数,为0时没有最大行数限制 自动换行   
    label1.numberOfLines = 2;    
//最小字体,行数为1时有效,默认为0.0    
    label1.minimumFontSize = 10.0;

//段 行 字间距
    label.numberOfLines = 0;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSKernAttributeName : @(1.5f)}];//字间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5.0];//设置行间距
    [paragraphStyle setParagraphSpacing:50.0];//设置段间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    label.attributedText = attributedString;

4、对齐方式、颜色

//设置文本对其方式    
   label1.textAlignment = UITextAlignmentCenter;    
//文本对齐方式有以下三种    
   //typedef enum {    
   //    UITextAlignmentLeft = 0,左对齐    
   //    UITextAlignmentCenter,居中对齐    
   //    UITextAlignmentRight, 右对齐                     
   //} UITextAlignment;    
 //注:现在的版本已经弃用上述对齐方式,以下方为主
 /* Values for NSTextAlignment */
typedef NS_ENUM(NSInteger, NSTextAlignment) {
   NSTextAlignmentLeft      = 0,    // Visually left aligned
#if TARGET_OS_IPHONE
   NSTextAlignmentCenter    = 1,    // Visually centered
   NSTextAlignmentRight     = 2,    // Visually right aligned
#else /* !TARGET_OS_IPHONE */
   NSTextAlignmentRight     = 1,    // Visually right aligned
   NSTextAlignmentCenter    = 2,    // Visually centered
#endif
   NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
   NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);

5、颜色

//文本颜色    
    label1.textColor = [UIColor blueColor];
//颜色渐变
    label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
//文本高亮    
    label1.highlighted = YES;
//高亮颜色
    label1.highlightedTextColor = [UIColor blueColor];    
//文本是否可变 :未激活,灰色,设置颜色无效
    label1.enabled = YES;    
//去掉label背景色    
    label1.backgroundColor = [UIColor clearColor];    
   
//文本阴影颜色    
    label1.shadowColor = [UIColor grayColor];    
//阴影偏移量   
    label1.shadowOffset = CGSizeMake(1.0, 1.0);

6、截取方式

//超出label边界文字的截取方式    
   label1.lineBreakMode = UILineBreakModeTailTruncation;    
//截取方式有以下6种    
   //typedef enum {           
   //    UILineBreakModeWordWrap = 0,    以空格为边界,保留整个单词             
   //    UILineBreakModeCharacterWrap,   保留整个字符             
   //    UILineBreakModeClip,            到边界为止             
   //    UILineBreakModeHeadTruncation,  省略开始,以……代替           
   //    UILineBreakModeTailTruncation,  省略结尾,以……代替          
   //    UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行           
   //} UILineBreakMode;

7、文本基线 自适应大小

//调整基线 需设置文本文字自适应大小
    label1.adjustsFontSizeToFitWidth = YES;    
//baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效    
    label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;    
//有三种方式    
//typedef enum {    
//    UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐    
//    UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐    
//    UIBaselineAdjustmentNone,//文本最低端与label中线对齐    
//} UIBaselineAdjustment;

8、获取label

/*
- (void)buttonClick:(UIButton*)button{
    //父视图通过tag值获取子视图的指针对象
    /*
     子视图可以设置一个tag值,然后添加到父视图上,父视图就可以通过这个tag值拿到子视图的指针。
     tag值也可以保存一些用户的信息。
     */
    UILabel* label = (UILabel*)[self.view viewWithTag:100];
    label.text = @"我被修改了";
}
*/

9、自适应高度

//自动适应高度
    CGSize size = CGSizeMake(280, 1000);
    UIFont *lableFont = [UIFont fontWithName:@"Arial" size:12.0];
    CGSize labelSize = [label.text sizeWithFont:lableFont constrainedToSize:size];//7.0 ,现如下
    [label1.text boundingRectWithSize:(CGSize) options:(NSStringDrawingOptions) attributes:(NSDictionary *) context:(NSStringDrawingContext *)]
    label.frame = CGRectMake(0, 0, 280, labelSize.height);
//或者
CGSize size = [label sizeThatFits:CGSizeMake(200, MAXFLOAT)];//根据文字的长度返回一个最佳宽度和高度  

10、富文本

//简单富文本
    NSString *text = @"我是大坏蛋哦!";
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor],   NSFontAttributeName : [UIFont systemFontOfSize:18]} range:NSMakeRange(2, 3)];
    label1.attributedText = attributeString;
//修改Range 所包含文本的 颜色、字体大小

11、圆角 边框

//圆角
    label.layer.backgroundColor =[UIColor yellowColor].CGColor;
    label.layer.cornerRadius =5.0;
    label.layer.frame = CGRectInset(label.layer.frame, 20, 20);
///边框
    label.layer.borderColor = [[UIColor redColor] CGColor];
    label.layer.borderWidth = 2;

12、添加到主视图

    [self.view addSubview:label1];    
    [label1 release];

不能循环改变text

自定义UILabel

上一篇下一篇

猜你喜欢

热点阅读