美文网首页Apple DeveloperiOS开发iOS Developer
iOS UIlable自定义lable 居上居左显示,

iOS UIlable自定义lable 居上居左显示,

作者: DoubleLine | 来源:发表于2016-12-05 17:46 被阅读171次

新建一个继承与UIlable的类如图:

创建myUILabel类

新建一个枚举表示(废话不说直接上代码)

typedef enum

{

VerticalAlignmentTop = 0,

VerticalAlignmentMiddle,

VerticalAlignmentBottom,

} VerticalAlignment;

#import "myUILabel.h"里面代码如下

#importtypedef enum

{

VerticalAlignmentTop = 0,

VerticalAlignmentMiddle,

VerticalAlignmentBottom,

} VerticalAlignment;

@interface myUILabel : UILabel{

@private

VerticalAlignment _verticalAlignment;

}

@property (nonatomic) VerticalAlignment verticalAlignment;

@end

#import "myUILabel.m"里面代码如下

#import "myUILabel.h"

@implementation myUILabel

@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:

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

在使用的时候只需要如下创建即可([demoLable setVerticalAlignment:VerticalAlignmentTop];

myUILabel*demoLable=[[myUILabel alloc]initWithFrame:CGRectMake(ScaleX(15), CGRectGetMaxY(lineView2.frame)+ScaleX(5),ScaleX(80), ScaleX(25) )];

[demoLable setVerticalAlignment:VerticalAlignmentTop];//让label居上显示

demoLable=[UIColor colorWithHexString:@"#666666"];

demoLable.textAlignment=NSTextAlignmentLeft;

demoLable.font=[UIFont systemFontOfSize:mytextfont];

demoLable.text=@"项目地址:";

//      AddressLable.backgroundColor=[UIColor purpleColor];

[self addSubview:demoLable];

相关文章

  • iOS UIlable自定义lable 居上居左显示,

    新建一个继承与UIlable的类如图: 新建一个枚举表示(废话不说直接上代码) typedef enum { Ve...

  • UILable 设置左右上下边距

    1:自定义UILable.h: .m: 2: 自定义后,发现lable如果为自适应的话,就会显示不全,解决方法: ...

  • UIlable的使用

    解决UILable显示有黑线 self.lable.layer.borderWidth = 0.2; self.l...

  • ImageView Scale Type 详解

    ImageView ScaleType 特点说明: fitStart居左或居上显示(视图片、Image宽高比而定)...

  • UILable,UITextfield

    UIlable //设置lable上的字 lable.text = @"zhang"; //设置lable中心对齐...

  • iOS 小知识

    UILabel 通过文字计算宽高 UILable *lable=[UILable alloc]init]; lab...

  • UILable、UITextField

    UI控件-UILable-UITextField 创建lable *新建lable *label的各种属性 *UI...

  • UILabel记录

    UILable添加中划线 如果lable中含有中文字符,可能会导致中划线无法显示,可加入NSBaselineOff...

  • UIButton文字居左显示

    UILabel文字居左显示,实现文字居左显示代码如下: 按照UILabel文字居左的写法,UIButton应该这么...

  • 16-1)UIButton开发中常见使用

    UIButton自定义图片文字位置 运行如下代码: 可以看出UIButton的默认显示样式是图片居左,文字居右,如...

网友评论

    本文标题:iOS UIlable自定义lable 居上居左显示,

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