Label的用法

发现UILabel好像没有点击事件之类的,今天算是搜到了一点点我想要的,还有要设置那个下划线之类的,还没有去查找呢,慢慢来吧

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UILabel *tmpLabel;

@end

@implementation ViewController
// MARK: jijijd
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化一个label
    //    CGRect label1Rect = CGRectMake(10, 10, 100, 100);
    //    UILabel *label1 = [[UILabel alloc] initWithFrame:label1Rect];
    //    label1.text = @"偶是label1";
    //    label1.font = [UIFont boldSystemFontOfSize:20];//设置字体大小
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
    //设置显示文字
    label1.text = @"label1";
    label2.text = @"label2";
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
    label4.text = @"label4--label4--label4--label4--";
    label5.text = @"label5--label5--label5--label5--label5--label5--";
    label6.text = @"label6";
    label7.text = @"label7";
    
    //设置字体:粗体,正常的是 SystemFontOfSize
    label1.font = [UIFont boldSystemFontOfSize:20];
    
    //设置文字颜色
    label1.textColor = [UIColor orangeColor];
    label2.textColor = [UIColor purpleColor];
    
    //设置文字位置
    label1.textAlignment =  NSTextAlignmentRight;
    label2.textAlignment =  NSTextAlignmentCenter;
    
    //设置字体大小适应label宽度 字体会变小 或者变大吧
    label4.adjustsFontSizeToFitWidth = YES;
    
    //设置label的行数
    label5.numberOfLines = 2;
    label5.backgroundColor = [UIColor clearColor];//可以去掉背景色
    
    //设置高亮
    label6.highlighted = YES;
    label6.highlightedTextColor = [UIColor orangeColor];
    
    //设置阴影
    label7.shadowColor = [UIColor redColor];
    label7.shadowOffset = CGSizeMake(1.0,1.0);
    
    //设置是否能与用户进行交互
    label7.userInteractionEnabled = YES;
    //设置label中的文字是否可变,默认值是YES
    label3.enabled = NO;
    
    //设置文字过长时的显示格式
    
    label3.lineBreakMode = NSLineBreakByTruncatingMiddle;//截去中间
    //  typedef enum {
    //      UILineBreakModeWordWrap = 0,
    //      UILineBreakModeCharacterWrap,
    //      UILineBreakModeClip,//截去多余部分
    //      UILineBreakModeHeadTruncation,//截去头部
    //      UILineBreakModeTailTruncation,//截去尾部
    //      UILineBreakModeMiddleTruncation,//截去中间
    //  } UILineBreakMode;
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
    label4.baselineAdjustment = UIBaselineAdjustmentNone;
    //  typedef enum {
    //      UIBaselineAdjustmentAlignBaselines,
    //      UIBaselineAdjustmentAlignCenters,
    //      UIBaselineAdjustmentNone,
    //  } UIBaselineAdjustment;     

    
    [self.view addSubview:label1];//添加label1
    [self.view addSubview:label2];
    [self.view addSubview:label3];
    [self.view addSubview:label4];
    [self.view addSubview:label5];
    [self.view addSubview:label6];
    [self.view addSubview:label7];
    
    NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
    myLabel.attributedText = attrStr;
    [self.view addSubview:myLabel];
    
    
    //label点击有交互事件产生
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 480.0, 200.0, 150.0)];
    l.text = @"点击点击点击 ";
    l.userInteractionEnabled=YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
    
    [l addGestureRecognizer:labelTapGestureRecognizer];
    [self.view addSubview:l];
    
//    label1.textColor  = UICol
}
//TODO:做todo事件
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label=(UILabel*)recognizer.view;
    
    NSLog(@"%@被点击了",label.text);
    
    
    
}
//FIXME:吧啦吧啦
- (void) doit{
    ;
}
@end
#pragma mark - 加载视图
- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect frame = CGRectMake(20 ,20 , 300, 300);
    self.myLabel = [[UILabel alloc]initWithFrame:frame];
    self.myLabel.text = @"呵呵哒";
    //设置字体
    self.myLabel.font = [UIFont boldSystemFontOfSize:20];
    //设置字体颜色
    self.myLabel.textColor = [UIColor orangeColor];
    //设置对齐方式
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    //设置字体大小适应Label宽度
    self.myLabel.adjustsFontSizeToFitWidth = YES;
    //设置行数
    self.myLabel.numberOfLines = 2;
    //设置阴影
    self.myLabel.shadowColor = [UIColor redColor];
    self.myLabel.shadowOffset = CGSizeMake(2.0, 2.0);
    //设置缩略显示方式
    self.myLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.view addSubview: self.myLabel];
}


让Label自适应,即为根据文字的高度来显示特定的高度

#pragma mark - 加载视图
- (void)viewDidLoad {
    [super viewDidLoad];
    self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)];
    //背景色
    self.myLabel.backgroundColor = [UIColor lightTextColor];
    //行数
    [self.myLabel setNumberOfLines:0];
    //缩略方式
    self.myLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    //字体
    UIFont *fonts = [UIFont fontWithName:@"Arial" size:12];
    self.myLabel.font = fonts;
    
    self.myLabel.text = @"ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室";
    int width = self.view.frame.size.width;
    int paddingLeft = 15;
    int paddingTop = 45;
    
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
    CGSize msgSize = [self.myLabel.text boundingRectWithSize:CGSizeMake(width - 2 * paddingLeft, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
   
    [self.myLabel setFrame:CGRectMake(paddingLeft, paddingTop, width - paddingLeft, msgSize.height)];
    [self.view addSubview:self.myLabel];
    
}

设置下划线删除线

    //显示中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName : [NSNumber numberWithInteger : NSUnderlineStyleSingle]};
    
    //显示下划线
    NSDictionary *attribtUnderlineDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger : NSUnderlineStyleSingle]};
    
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.myLabel.text attributes:attribtUnderlineDic];
    self.myLabel.attributedText = attribtStr;





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值