iOS 开发中的细节知识点之UILabel篇

UILabel:

 UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 20)];

——>Label字体居上显示(方法一:在字符串末尾用回车补齐)

// 对应字号的字体一行显示所占宽高
CGSize fontSize = [Label.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
// 对应字号的内容实际所占范围
CGSize stringSize = [Label.text boundingRectWithSize:CGSizeMake(Label.frame.size.width, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;
// 剩余空行
NSInteger line = (Label.frame.size.height - stringSize.height) / fontSize.height;
// 用回车补齐
for (int i = 0; i < line; i++) {
    Label.text = [Label.text stringByAppendingString:@"\n "];
    }
Label.numberOfLines=0;

——>Label字体居上或居下显示(方法二:重写一个继承UILabel的类)

//在.h中(该类是继承于UILabel的)
#import <UIKit/UIKit.h>
typedef enum VerticalAlignment {
    VerticalAlignmentTop,
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;
@interface JXLabel : UILabel
{
@private
VerticalAlignment verticalAlignment_;
}
@property (nonatomic, assign) VerticalAlignment verticalAlignment;
@end

//在.m中
#import "JXLabel.h"
@implementation JXLabel
@synthesize verticalAlignment = verticalAlignment_;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}
- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalAlignment) {
        case VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalAlignmentMiddle:
            // Fall through.
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}
@end
//使用方法:
//使用该类初始化Label(如:JXLabel * Label=[[JXLabel alloc]init];)
//只要加上下面一行代码即可实现文字居上显示:
Label.verticalAlignment = VerticalAlignmentTop;//文字居上显示
Label.verticalAlignment = VerticalAlignmentBottom;//文字居下显示

——>字体加阴影模糊效果

NSShadow *shadow=[[NSShadow alloc]init];
shadow.shadowBlurRadius=4;//模糊度
shadow.shadowColor=[UIColor blackColor];//阴影颜色
shadow.shadowOffset=CGSizeMake(0, 0);//阴影偏移x(正值)向左偏y(正值)向下偏
//绘制文本
NSDictionary *dict=@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSShadowAttributeName:shadow,NSVerticalGlyphFormAttributeName:@(0)};
Label.attributedText = [[NSAttributedString alloc]initWithString:Label.text attributes:dict];
//设置文本字体
UIFont *kaiFont=[UIFont fontWithName:@"STHeitiSC-Light" size:20];
[Label setFont:kaiFont];

——>调整字体行间距

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:Label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:7];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [Label.text length])];
Label.attributedText = attributedString;
[Label sizeToFit];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值