我是目标c的新手,我需要一些帮助来创建一个标签,该标签出现在我的所有ViewControllers中,文本相同,但此文本将不断更改。
谢谢你们。
答案 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
//
// 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
然后在视图控制器中,您可以调用此