iOS控件--UILabel

UILabel继承与UIView,是iOS设计非常常用的一个控件

//label初始化
    UILabel *lable = [[UILabel alloc] init];
    //label的位置
    lable.frame = CGRectMake(20, 100, 300, 50);
    //label的内容
    lable.text = @"这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是最后几个结尾字";
    //label的背景颜色
    lable.backgroundColor = [UIColor yellowColor];
    //label内容的颜色
    lable.textColor = [UIColor redColor];
    //label字体的大小
    lable.font = [UIFont systemFontOfSize:30];
    
    //设置阴影颜色
    lable.shadowColor = [UIColor blackColor];
    //label阴影的偏移量
    lable.shadowOffset = CGSizeMake(3, 3);
    
    //label的对齐方式
    lable.textAlignment = NSTextAlignmentCenter;
    //label的边缘属性
    lable.layer.masksToBounds = YES;
    //label的圆角
    lable.layer.cornerRadius = 20;
    //label边线颜色
    lable.layer.borderColor = [[UIColor purpleColor] CGColor];
    //label边线宽度
    lable.layer.borderWidth = 3;
    //文字超长处理
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.view addSubview:lable];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 200, 50)];
    label2.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label2];
    //==================
    //多属性字符串的使用:$700 ,$是红色的,700是黑色的
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]init];
    //NSFontAttributeName字典的key,代表字体
    //NSForegroundColorAttributeName 文字颜色
    //NSBackgroundColorAttributeName 文字背景色
    NSAttributedString *str1 =[[NSAttributedString alloc]initWithString:@"$" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:35],NSForegroundColorAttributeName:[UIColor redColor]}];
    NSAttributedString *str2 = [[NSAttributedString alloc]initWithString:@"700" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    [attrString appendAttributedString:str1];
    [attrString appendAttributedString:str2];
    label2.attributedText = attrString;
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, 200, 250)];
    [self.view addSubview:label3];
    label3.backgroundColor = [UIColor yellowColor];
    //用户可交互
    label3.userInteractionEnabled = YES;
    //label高亮
    label3.highlighted = YES;
    //label高亮颜色
    label3.highlightedTextColor = [UIColor purpleColor];
    //label字体大小适应label宽度(与numberOfLines冲突)
    label3.adjustsFontSizeToFitWidth = YES;
    label3.text = @"这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是最后几个结尾字";
    //多行控制为0时,表示多行
    label3.numberOfLines = 0;

对齐方式textAlignment:

    NSTextAlignmentLeft      = 0,    // Visually left aligned   左对齐

    NSTextAlignmentCenter    = 1,    // Visually centered    居中

    NSTextAlignmentRight     = 2,    // Visually right aligned  右对齐

    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.

    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script

文字超长处理lineBreakMode:

    NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default 包在用词边界(默认)应该是能够识别空格

    NSLineBreakByCharWrapping, // Wrap at character boundaries            包在字符边界

    NSLineBreakByClipping, // Simply clip                                                直接截取

    NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz"   头部缩略

    NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..."         尾部缩略

    NSLineBreakByTruncatingMiddle // Truncate middle of line:  "ab...yz"   中间缩略

文本基线控制baselineAdjustment:

    UIBaselineAdjustmentAlignBaselines = 0, // default. 默认文本最上端与label中间线

    UIBaselineAdjustmentAlignCenters,         //text中间与label中间线对齐

    UIBaselineAdjustmentNone,                   //text最底端与label中间线对齐           







转载于:https://my.oschina.net/neilWXD/blog/648626

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值