在xcode中的所有视图中创建标签

时间:2014-12-31 04:34:45

标签: objective-c xcode

我是目标c的新手,我需要一些帮助来创建一个标签,该标签出现在我的所有ViewControllers中,文本相同,但此文本将不断更改。

谢谢你们。

1 个答案:

答案 0 :(得分:0)

您可以为此

创建Category类 .h文件中的

//
//  UILabel+withString.h
//

#import <Foundation/Foundation.h>

@interface UILabel (withString)

+ (UILabel *)labelWithString:(NSString *)string
                    font:(UIFont *)font
                   color:(UIColor *)color
               container:(CGRect)container
                  origin:(CGPoint)origin;

@end

在.m文件

//
//  UILabel+withString.m
//

 #import "UILabel+withString.h"


 @implementation UILabel (withString)

 + (UILabel *)labelWithString:(NSString *)string
                    font:(UIFont *)font
                   color:(UIColor *)color
               container:(CGRect)container
                  origin:(CGPoint)origin {
 CGSize size = [string sizeWithFont:font constrainedToSize:container.size
    lineBreakMode:UILineBreakModeTailTruncation];

 UILabel *label = [[[UILabel alloc]
    initWithFrame:CGRectMake(origin.x, origin.y, container.size.width, size.height)] autorelease];
 label.text = string;
 label.font = font;
 label.textColor = color;
 label.textAlignment = UITextAlignmentLeft;
 label.numberOfLines = 1;
 label.lineBreakMode = UILineBreakModeTailTruncation;
  label.backgroundColor = [UIColor clearColor];

 return label;
 }

 @end

然后在视图控制器中,您可以调用此