可以滚动的Label标签

来源:互联网 发布:打印机网络共享器 编辑:程序博客网 时间:2024/05/21 07:54

虽然现在手机屏幕越来越大,但是手机屏幕毕竟有限,而各种各样的信息越来越缤纷复杂,所以合理利用手机屏幕尤为重要。

我们经常遇到,在有限的空间里,只能放下有限大小的Label标签,但是需要展示的信息却很多,这样一来,可以滚动的Label标签就可以尽量用最小的布局展示更大的信息量。

下面就自己实现一个这样的可以滚动的Label标签。

先创建一个继承于UISrollView的RollLabel类

#import<UIKit/UIKit.h>

#define kConstrainedSize CGSizeMake(10000,40)

@interface RollLabel :UIScrollView


@end


搞一个“+”方法,方便外部直接调用,然后将实现方法全写到RollLabel.m文件中

+ (void)rollLabelTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font superView:(UIView *)superView fram:(CGRect)rect;


RollLabel.m文件中的全部实现方法如下

- (id)initWithFrame:(CGRect)frame Withsize:(CGSize)size

{

    self = [superinitWithFrame:frame];

    if (self) {

       self.showsVerticalScrollIndicator   =NO;

       self.showsHorizontalScrollIndicator =NO;//水平滚动条

        self.contentSize = size;//滚动大小

       self.backgroundColor = [UIColorcolorWithHue:0.1saturation:0.6brightness:1.0alpha:1.0];

    }

    return self;

}

+ (void)rollLabelTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font superView:(UIView *)superView fram:(CGRect)rect {

    NSMutableParagraphStyle *style=[[NSMutableParagraphStyledefaultParagraphStyle]mutableCopy];

    [stylesetLineBreakMode:NSLineBreakByWordWrapping];

    

    NSDictionary *dis=[NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName, style,NSParagraphStyleAttributeName,nil];

    //文字大小,设置label的大小和uiscroll的大小

    CGSize size=[titleboundingRectWithSize:kConstrainedSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:dis context:nil].size;

    

    CGRect frame =CGRectMake(0,0, size.width, rect.size.height);

    RollLabel *roll = [[RollLabelalloc]initWithFrame:rectWithsize:size];

    UILabel *label = [[UILabelalloc]initWithFrame:frame];

    label.text = title;

    label.font = font;

    label.textColor = color;

    [roll addSubview:label];

  

    [superView addSubview:roll];

 

}


导入RollLabel.h文件,调用“+”方法。

#import"RollLabel.h"


[RollLabelrollLabelTitle:@"有时候,我们错过的不是时间,是感觉"color:[UIColorblackColor] font:[UIFontsystemFontOfSize:30]superView:self.viewfram:CGRectMake(60,150, 200, 40)];


OK,搞定,快来试试吧。

1 0
原创粉丝点击