美文网首页iOS开发之控件
UILabel - 简单整理

UILabel - 简单整理

作者: 居然是村长 | 来源:发表于2016-03-26 10:25 被阅读124次

    虽然很基本简单,但还是有那么几个比较少用的属性,今天在用cell时,竟然关系到Label 的属性,在此整理下。

    常用基本属性

    • 最常用
        label.text = @"wertyu;";
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
    
    • 格式调整
        label.numberOfLines = 1;
        label.lineBreakMode = NSLineBreakByCharWrapping;
        /*
         typedef NS_ENUM(NSInteger, NSLineBreakMode) {
         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"
         };
         */
        label.adjustsFontSizeToFitWidth = NO;// 根据宽度调整字体大小
        label.minimumScaleFactor = 0.2;// 代替了原来的最小字体大小,新的是比例
        label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;// 正常的baseLine(可以调整)
        /*
         typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
         UIBaselineAdjustmentAlignBaselines = 0,
         UIBaselineAdjustmentAlignCenters,
         UIBaselineAdjustmentNone,
         };
         */
    
    • 加特效
        // 属性字符串(具体的看 NSAttributedString 类)
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],
                                     NSForegroundColorAttributeName:[UIColor redColor]};
        label.attributedText = [[NSAttributedString alloc] initWithString:@"3456789" attributes:attributes];
        
        // 阴影
        label.shadowColor = [UIColor greenColor];
        label.shadowOffset = CGSizeMake(2, 2);
        
    
    • 高亮
        label.highlightedTextColor = [UIColor greenColor];
        label.highlighted = YES;// 这个真是坑爹啊,在cell点击效果中添加label 高亮颜色,默认就设为YES了,改一下上面的highlightedTextColor 就可以了。
    
    • 可用性
        label.userInteractionEnabled = YES;// 是否可以交互,默认NO,开启可添加手势
        label.enabled = NO;// 只感觉有变灰效果。。默认YES,可用
    
    • 重绘
    - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;// 调整rect,比如第一行缩进多少的语文课代表形式。
    - (void)drawTextInRect:(CGRect)rect;// 画text
    
    • 自动布局相关
    preferredMaxLayoutWidth = 100;// ios6,需要设置,label的宽是多少,然后才能自动换行计算高度,现在不需要了。
    

    其他

    @property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // default is NO
    

    相关文章

      网友评论

        本文标题:UILabel - 简单整理

        本文链接:https://www.haomeiwen.com/subject/knedlttx.html