IOS UILabel

mas抗拉伸抗压缩

概念简述
约束优先级: 在Autolayout中每个约束都有一个优先级, 优先级的范围是1 ~ 1000。创建一个约束,默认的优先级是最高的1000;Content Hugging Priority: 该优先级表示一个控件抗被拉伸的优先级。优先级越高,越不容易被拉伸,默认是250(UILayoutPriorityDefaultLow)。
Content Compression Resistance Priority: 该优先级和上面那个优先级相对应,表示一个控件抗压缩的优先级。优先级越高,越不容易被压缩,默认是750(UILayoutPriorityDefaultHigh);
通过以下两个方法来设置他们的抗被拉伸的优先级和抗压缩的优先级:

  • (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis API_AVAILABLE(ios(6.0));

  • (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
    优先级取值:
    static const UILayoutPriority UILayoutPriorityRequired = 1000;
    static const UILayoutPriority UILayoutPriorityDefaultHigh = 750;
    static const UILayoutPriority UILayoutPrioritySceneSizeStayPut = 500;
    static const UILayoutPriority UILayoutPriorityDefaultLow = 250;
    static const UILayoutPriority UILayoutPriorityFittingSizeLevel = 50;
    场景一

  • (void)test {

    UILabel *leftLabel = [UILabel new];
    leftLabel.text = @“我是左边我是我是左边”;
    leftLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:leftLabel];

    UILabel *rightLabel = [UILabel new];
    rightLabel.text = @“评论数:20”;
    rightLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:rightLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(100);
    make.left.mas_equalTo(10);
    }];

    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(leftLabel.mas_right).offset(10);
    make.right.mas_equalTo(-10);
    make.top.equalTo(leftLabel.mas_top);
    }];
    }
    默认显示效果如图
    如果希望左边的自适应,右边的自动填充,那么我们需要增大左边的抗被拉伸优先级(越大越不容易被拉伸,默认250、UILayoutPriorityDefaultLow),或者减小右边的抗被拉伸优先级,这样才能让右边的label内容优先显示。
    增加以下代码设置:
    [leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    或者
    [rightLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
    显示效果
    在增加上述的代码以后如果增加左边的文字,效果如下:
    此时发现右边的文字无法显示了,继续向下分析;
    场景二

  • (void)test {

    UILabel *leftLabel = [UILabel new];
    leftLabel.text = @“我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边”;
    leftLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:leftLabel];

    UILabel *rightLabel = [UILabel new];
    rightLabel.text = @“评论数:20”;
    rightLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:rightLabel];

    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(100);
    make.left.mas_equalTo(10);
    }];

    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(leftLabel.mas_right).offset(10);
    make.right.mas_equalTo(-10);
    make.top.equalTo(leftLabel.mas_top);
    }];
    }
    左边内容多超过屏幕,默认显示效果如图
    如果希望右边完全显示,左边内容自适应,那么我们需要增大右边的抗压缩优先级(优先级越高,越不容易被压缩,默认750、UILayoutPriorityDefaultHigh),或者减少左边的抗压缩优先级;
    增加以下代码设置:
    [leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    或者
    [rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]
    显示效果
    在增加上述的代码以后如果,减少左边的文字,效果如下:
    场景三
    需求:

  1. 黄色内容完全展示并且在红色右边,并且间距始终为10
  2. 红色内容少的时候自适应、内容多的时候显示…
    解决方案:
    这个时候需要将场景一和场景2的内容同时使用,也就是同时使用抗压缩和抗拉伸优先级:
    [leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]
    或者
    [rightLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
    [leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    即场景一和场景二中的代码任性其一进行组合,这里只列出了2种;
    运行效果1(左边内容多的时候):
    运行效果2(左边内容少的时候):
    总结
    • 空间充足的情况下,可以设置某个控件的抗被拉伸优先级,让某个控件或其余控件占用剩余空间;
    • 空间不足的情况下,可以设置某个控件的抗压缩优先级,让某个控件展示完全;
    jueUILabel自适应

    UILabel* labelJinE = [[UILabel alloc]init];
    labelJinE.text = @"到账金额:--元";
    
    labelJinE.font = [UIFont systemFontOfSize:16];
    [labelJinE sizeToFit];
    labelJinE.center = CGPointMake((WINDOW_WIDTH-ButtonWide*3)/4*1+labelJinE.bounds.size.width/2, 72/2);
    [viewButtom addSubview:labelJinE];

其中[labelJinE sizeToFit] 要写在center之前,否则先设置了center,后在自适应frame,会有些偏差,如果要加上确定的背景颜色的,代码如下

    UILabel* labelJinE = [[UILabel alloc]init];
    labelJinE.text = @"到账金额:--元";
    labelJinE.font = [UIFont systemFontOfSize:16];
     labelJinE.bounds = CGRectMake(0, 0, 96, 44);(绝对宽高)
    labelJinE.center = CGPointMake((WINDOW_WIDTH-ButtonWide*3)/4*1+labelJinE.bounds.size.width/2, 72/2);
    [viewButtom addSubview:labelJinE];

其中设置buounes需要在center之前,不需要添加sizeToFit,前提确保你的bounds的wide 和height 是绝对的,其中内容不会自适应的,需要自己根据需要调动,可用户固定view位置,固定button背景色
label随文本高度自适应的话,需要加上sizeToFit,高度可为MAXFLOAT
下面附上便捷的自定义label

/*
 只返回一行的label
 isRelativeCoordinate:是否是相对坐标(即加上自身的Wide,Hight的一半)
 */
+(UILabel *)commonLabelWithText:(NSString*)text  withFontSize:(CGFloat)fontSize withOriginX:(CGFloat)x withOriginY:(CGFloat)y isRelativeCoordinate:(BOOL)isRC{
    UILabel* label =[[UILabel alloc]init];
    label.text =text;
    label.font = [UIFont systemFontOfSize:fontSize];
    [label sizeToFit];
    if (isRC) {
          label.center = CGPointMake(x+label.bounds.size.width/2, y+label.bounds.size.height/2);
    }else{
    label.center = CGPointMake(x,y);
    }
    return label;

}
/*
 返回多行的label
 相对坐标(即加上自身的Wide,Hight的一半)
 */
+(UILabel *)commonMoreLabelWithText:(NSString*)text withFontSize:(CGFloat)fontSize withBoundsWide:(CGFloat)boundsWide withOriginX:(CGFloat)x withOriginY:(CGFloat)y{
    UILabel* label =[[UILabel alloc]init];
    label.text =text;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:fontSize];
    label.bounds = CGRectMake(0, 0, boundsWide, MAXFLOAT);
    [label sizeToFit];
    label.center = CGPointMake(x+label.bounds.size.width/2, y+label.bounds.size.height/2);

    return label;

}

UILabel全属性

#import "ViewController.h"
#import <CoreText/CoreText.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //学习内容
    /*
     1.控件 UIView UILabel UITextField UITextView UIButton
     2.字体、大小、单位、颜色
     */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
    label.text = @"Label Text Content, This is a text label things attribute";//默认为空
    label.font = [UIFont systemFontOfSize:17];//默认使用系统的17
    label.textColor = [UIColor orangeColor];//默认使用文本黑色
    label.shadowColor = [UIColor lightGrayColor];//默认没有阴影
    label.shadowOffset = CGSizeMake(1,0);//默认是一个向上的阴影(0,-1)
    label.textAlignment = NSTextAlignmentCenter;//默认是左对齐
    label.lineBreakMode = NSLineBreakByTruncatingTail;//段落样式,默认是最后截断尾巴,用...代替
    
    //富文本的基本数据类型,属性字符串。以此为基础,如果这个设置了相应的属性,则会忽略上面设置的属性,默认为空
    NSString *string = label.text;
    const CGFloat fontSize = 16.0;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSUInteger length = [string length];
    //设置字体
    UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//设置所有的字体
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//设置Text这四个字母的字体为粗体
    //设置倾斜,需要导入coreText
    UIFont *italicFont = GetVariationOfFontWithTrait(baseFont,
                                                     kCTFontTraitItalic);
    [attrString addAttribute:NSFontAttributeName value:italicFont
                       range:[string rangeOfString:@"Label"]];
    // 设置颜色
    UIColor *color = [UIColor redColor];
    [attrString addAttribute:NSForegroundColorAttributeName
                       value:color
                       range:[string rangeOfString:@"Content"]];
    [attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]];
    
    //可以对这些属性设置值
    //字体名称有以下:
//    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24];
    [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]];
    label.numberOfLines = 2;
    NSMutableParagraphStyle *
    style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineSpacing = 10;//增加行高
    style.headIndent = 10;//头部缩进,相当于左padding
    style.tailIndent = -10;//相当于右padding
    style.lineHeightMultiple = 1.5;//行间距是多少倍
    style.alignment = NSTextAlignmentLeft;//对齐方式
    style.firstLineHeadIndent = 20;//首行头缩进
    style.paragraphSpacing = 10;//段落后面的间距
    style.paragraphSpacingBefore = 20;//段落之前的间距
    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
    
    [attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];//字符间距 2pt
    [attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心
    [attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//空心字,文字边框描述
    [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];//下划线
    [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];//厚的下划线
    [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];//删除线
    [attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];//删除线蓝色
   label.attributedText = attrString;
    
    label.highlightedTextColor = [UIColor redColor];//设置文本高亮显示颜色,与highlighted一起使用。
    label.highlighted = NO; //高亮状态是否打开
    label.enabled = YES;//设置文字内容是否可变
    label.userInteractionEnabled = YES;//设置标签是否忽略或移除用户交互。默认为NO
    label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
//    UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。
//    UIBaselineAdjustmentAlignCenters,   文本中线与label中线对齐。
//    UIBaselineAdjustmentNone,  文本最低端与label中线对齐。;
    [self.view addSubview:label];
    
    /*
     字体名如下:
     Font Family: American Typewriter
     Font: AmericanTypewriter
     Font: AmericanTypewriter-Bold
     
     Font Family: AppleGothic
     Font: AppleGothic
     
     Font Family: Arial
     Font: ArialMT
     Font: Arial-BoldMT
     Font: Arial-BoldItalicMT
     Font: Arial-ItalicMT
     
     Font Family: Arial Rounded MT Bold
     Font: ArialRoundedMTBold
     
     Font Family: Arial Unicode MS
     Font: ArialUnicodeMS
     
     Font Family: Courier
     Font: Courier
     Font: Courier-BoldOblique
     Font: Courier-Oblique
     Font: Courier-Bold
     
     Font Family: Courier New
     Font: CourierNewPS-BoldMT
     Font: CourierNewPS-ItalicMT
     Font: CourierNewPS-BoldItalicMT
     Font: CourierNewPSMT
     
     Font Family: DB LCD Temp
     Font: DBLCDTempBlack
     
     Font Family: Georgia
     Font: Georgia-Bold
     Font: Georgia
     Font: Georgia-BoldItalic
     Font: Georgia-Italic
     
     Font Family: Helvetica
     Font: Helvetica-Oblique
     Font: Helvetica-BoldOblique
     Font: Helvetica
     Font: Helvetica-Bold
     
     Font Family: Helvetica Neue
     Font: HelveticaNeue
     Font: HelveticaNeue-Bold
     
     Font Family: Hiragino Kaku Gothic **** W3
     Font: HiraKakuProN-W3
     
     Font Family: Hiragino Kaku Gothic **** W6
     Font: HiraKakuProN-W6
     
     Font Family: Marker Felt
     Font: MarkerFelt-Thin
     
     Font Family: STHeiti J
     Font: STHeitiJ-Medium
     Font: STHeitiJ-Light
     
     Font Family: STHeiti K
     Font: STHeitiK-Medium
     Font: STHeitiK-Light
     
     Font Family: STHeiti SC
     Font: STHeitiSC-Medium
     Font: STHeitiSC-Light
     
     Font Family: STHeiti TC
     Font: STHeitiTC-Light
     Font: STHeitiTC-Medium
     
     Font Family: Times New Roman
     Font: TimesNewRomanPSMT
     Font: TimesNewRomanPS-BoldMT
     Font: TimesNewRomanPS-BoldItalicMT
     Font: TimesNewRomanPS-ItalicMT
     
     Font Family: Trebuchet MS
     Font: TrebuchetMS-Italic
     Font: TrebuchetMS
     Font: Trebuchet-BoldItalic
     Font: TrebuchetMS-Bold
     
     Font Family: Verdana
     Font: Verdana-Bold
     Font: Verdana-BoldItalic
     Font: Verdana
     Font: Verdana-Italic
     
     Font Family: Zapfino
     Font: Zapfino
     */
    
    //文本对齐方式
    /* Values for NSTextAlignment */
    /*
    NSTextAlignmentLeft 左对齐
    NSTextAlignmentCenter 剧中对齐
    NSTextAlignmentRight 右对齐
    NSTextAlignmentJustified 两端对齐
    NSTextAlignmentNatural 根据显示的文字特性对齐
    */
    
    //段落样式
    /*
    lineSpacing;                         来增加行距
    paragraphSpacing;
    alignment;                           对齐
    firstLineHeadIndent;                 段落开始的缩排像素
    headIndent;                          可调整全部文字的缩排距离,可当作左边 padding 使用
    tailIndent;                          可调整文字尾端的缩排距离。需要注意的是,这里指定的值可以当作文字显示的宽、而也可当作右边 padding 使用,依据输入的正负值而定:
    lineBreakMode;
    minimumLineHeight;
    maximumLineHeight;        而针对不同的字型与字号,我们可以透过指定最大与最小行距(maximumLineHeight 与 minimumLineHeight)来避免过高或过窄的状况发生。
    baseWritingDirection;
    lineHeightMultiple;                  想要调整行距,可以透过搭配使用 lineHeightMultiple 更改行距倍数
    paragraphSpacingBefore; 而若是文章内容有分段落的话,也可以透过指定段落结尾距离(paragraphSpacing)以及段落开头距离(paragraphSpacingBefore):
    hyphenationFactor;
        @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
        @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
     */
    
    /* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.  //预定义的文本属性值,如果键不是一个字典,那么使用默认的值作为以下描述
     NSFontAttributeName 字体 默认是Helvetica 12号
     NSParagraphStyleAttributeName 段落样式
     */
    /*
    UIKIT_EXTERN NSString *const  NS_AVAILABLE_IOS(6_0);      // NSParagraphStyle, default defaultParagraphStyle
    UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default blackColor
    UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default nil: no background
    UIKIT_EXTERN NSString *const NSLigatureAttributeName NS_AVAILABLE_IOS(6_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
    UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0);                // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)
    UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0);  // NSNumber containing integer, default 0: no strikethrough
    UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0);      // NSNumber containing integer, default 0: no underline
    UIKIT_EXTERN NSString *const NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0);         // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0);         // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
    UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0);              // NSShadow, default nil: no shadow
    UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0);          // NSString, default nil: no text effect

    UIKIT_EXTERN NSString *const NSAttachmentAttributeName NS_AVAILABLE_IOS(7_0);          // NSTextAttachment, default nil
    UIKIT_EXTERN NSString *const NSLinkAttributeName NS_AVAILABLE_IOS(7_0);                // NSURL (preferred) or NSString
    UIKIT_EXTERN NSString *const NSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0);      // NSNumber containing floating point value, in points; offset from baseline, default 0
    UIKIT_EXTERN NSString *const NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0);      // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString *const NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0);  // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString *const NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0);         // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
    UIKIT_EXTERN NSString *const NSExpansionAttributeName NS_AVAILABLE_IOS(7_0);           // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

    UIKIT_EXTERN NSString *const NSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0);    // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values.  LRE: NSWritingDirectionLeftToRight|NSTextWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSTextWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride,

    UIKIT_EXTERN NSString *const NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0);   // An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.
     */
    
    // NSParagraphStyle  段落样式
//    typedef NS_ENUM(NSInteger, NSLineBreakMode) {		/* What to do with long lines */ //对于长内容或多行内容的处理方式
//        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" */        //截断中间
//    } NS_ENUM_AVAILABLE_IOS(6_0);
}
//获取斜体
UIFont * GetVariationOfFontWithTrait(UIFont *baseFont,
                                     CTFontSymbolicTraits trait) {
    CGFloat fontSize = [baseFont pointSize];
    CFStringRef
    baseFontName = (__bridge CFStringRef)[baseFont fontName];
    CTFontRef baseCTFont = CTFontCreateWithName(baseFontName,
                                                fontSize, NULL);
    CTFontRef ctFont =
    CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL,
                                       trait, trait);
    NSString *variantFontName =
    CFBridgingRelease(CTFontCopyName(ctFont,
                                     kCTFontPostScriptNameKey));

    UIFont *variantFont = [UIFont fontWithName:variantFontName
                                          size:fontSize];
    CFRelease(ctFont);
    CFRelease(baseCTFont);
    return variantFont;
};


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Label增加间距

   NSString* str =@"说明: 绑定后你可以直接使用手机号登陆热波ST以保障账号安全。你的手机号将严格保密,不会泄露给第三方!";
    UILabel *warncontent = [[UILabel alloc] initWithFrame:CGRectMake(21, 512/2+3 , SCREEN_WIDTH - 21 -10 , 60)];
    warncontent.font = [UIFont systemFontOfSize:12.0f];
    warncontent.textColor = [CommonFuction colorFromHexRGB:@"575757"];
    warncontent.numberOfLines = 0;
    
    
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
    [warncontent setAttributedText:attributedString];
    [warncontent sizeToFit];
    warncontent.lineBreakMode = NSLineBreakByWordWrapping;
    [self.view addSubview:warncontent];

加描边

继承UILabel以后重载drawTextInRect

- (void)drawTextInRect:(CGRect)rect {

   CGSize shadowOffset = self.shadowOffset;
   UIColor *textColor = self.textColor;

   CGContextRef c = UIGraphicsGetCurrentContext();
   CGContextSetLineWidth(c, 1);
   CGContextSetLineJoin(c, kCGLineJoinRound);

   CGContextSetTextDrawingMode(c, kCGTextStroke);
   self.textColor = [UIColor whiteColor];
   [super drawTextInRect:rect];

   CGContextSetTextDrawingMode(c, kCGTextFill);
   self.textColor = textColor;
   self.shadowOffset = CGSizeMake(0, 0);
   [super drawTextInRect:rect];

   self.shadowOffset = shadowOffset;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值