美文网首页
iOS Label属性全解

iOS Label属性全解

作者: 燃烧的蔬菜C | 来源:发表于2018-04-24 14:54 被阅读0次

    记录label一些基本属性,包括常用的文字自适应高度,行间距等基本使用方法

    /** 设置高亮文本颜色,与highlighted属性一起使用,只有设置highlighted为YES才生效 */

    self.label.highlightedTextColor = [UIColor redColor];

    /** 设置行数,0为多行 */

    self.label.numberOfLines = 1;

    /** 默认为NO,一般添加手势才会设置为YES */

    self.label.userInteractionEnabled = YES;

    /** 默认为YES,若设置为NO,文字颜色会变暗,而且高亮属性highlighted失效 */

    self.label.enabled = YES;

    /** 设置阴影 */

    self.label.shadowColor = [UIColor orangeColor];

    self.label.shadowOffset = CGSizeMake(1, 2);

    /** 文本对齐方式

        NSTextAlignmentLeft  默认左对齐

        NSTextAlignmentCenter  居中

        NSTextAlignmentRight  右对齐

    */

    self.label.textAlignment = NSTextAlignmentLeft;   // 左对齐

    /**  在固定的宽高度下,自动调整字体大小,以全部显示字体 。可根据字数来调整字体大小,值是0~1,与adjustsFontSizeToFitWidth属性一起使用和行数为1的时候才有效 */

    self.label.minimumScaleFactor = 8.0/[UIFont labelFontSize];

    self.label.adjustsFontSizeToFitWidth = YES;

    /** 如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。

        UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与label中线对齐

        UIBaselineAdjustmentAlignCenters,文本中线与label中线对齐

        UIBaselineAdjustmentNone,文本最低端与label中线对齐

    */

    self.label.baselineAdjustment = UIBaselineAdjustmentNone;

    /* 设置标签文字过长时的显示方式

        NSLineBreakByWordWrapping = 0, 以单词为单位显示,超出部分省略不显示 (adjustsFontSizeToFitWidth属性失效)

        NSLineBreakByCharWrapping,以字符为显示单位显示,超出部分省略不显示 (adjustsFontSizeToFitWidth属性失效)

        NSLineBreakByClipping,剪切与文本宽度相同的内容长度,超出部分不显示

        NSLineBreakByTruncatingHead,Truncate at head of line: "...wxyz"

        NSLineBreakByTruncatingTail,Truncate at tail of line: "abcd..."

        NSLineBreakByTruncatingMiddleTruncate middle of line:  "ab...yz"

    */

    self.label.lineBreakMode = NSLineBreakByClipping;

    /** 基本不用的属性 */

    self.label.minimumFontSize = 50;

    self.label.allowsDefaultTighteningForTruncation = YES;

    self.label.adjustsLetterSpacingToFitWidth = YES;

    self.label.preferredMaxLayoutWidth = self.view.frame.size.width;

    // 点击按钮后设置highlighted和attributedText两个属性

    - (IBAction)action:(UIButton *)sender {

        self.label.highlighted = !self.label.highlighted;

        /** 设置文本样式

            NSFontAttributeName 字体默认是12号

            NSParagraphStyleAttributeName 段落样式

            NSForegroundColorAttributeName 指定字体颜色

            NSBackgroundColorAttributeName 字体背景颜色

            NSLigatureAttributeName 连体字符

            NSKernAttributeName 字距的像素

            NSStrikethroughStyleAttributeName指定字上加删除线

            NSUnderlineStyleAttributeName 指定字加下划线

            NSStrokeColorAttributeName 填充部分颜色

            NSStrokeWidthAttributeName 填充宽度

            NSShadowAttributeName 阴影

            NSUnderlineColorAttributeName 下划线颜色

        */

        NSMutableAttributedString *attributed = [[NSMutableAttributedStringalloc]initWithString:self.label.text];

        // 设置第一个字符的颜色为黑色

        [attributed addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblackColor]range:NSMakeRange(0,1)];

        self.label.attributedText = attributed;

    }

    /**

    * 根据文字自动设置size

     */

    - (CGSize)getCurrentTextSize {

            CGSize size =CGSizeMake(MAXFLOAT,MAXFLOAT);

            if (self.label.text.length > 0) {

                #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >=70000

                    size = [self.label.textboundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin

                    attributes:@{NSFontAttributeName:self.label.font} context:nil].size;

                #else

                    size = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];

                #endif

        }

        return size;

    }

    /** 设置行间距 */

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.lineSpacing = 15;

    NSDictionary*attrbut=@{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:paragraphStyle};

    NSMutableAttributedString*attributedText=[[NSMutableAttributedStringalloc]initWithString:self.text attributes:attrbut];

    label.attributedText = attributedText;

    PS:适用初学者,希望能帮助到需要的人,如有不对,欢迎指点!

    相关文章

      网友评论

          本文标题:iOS Label属性全解

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