如何制作跨多个 View 的方法?例如。我创建了这个:
- (void)setPageTitle:(UILabel *)title withText:(NSString *)text
{
UIColor *pageTextColor = [UIColor colorWithRed:18.0/255.0 green:79.0/255.0 blue:118.0/255.0 alpha:1.0];
// Set page title
UIFont *font = [UIFont fontWithName:@"PassionOne-Regular" size:23];
[title setFont:font];
[title setText: text];
title.textColor = pageTextColor;
title.shadowColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
title.shadowOffset = CGSizeMake(0, 1);
CGRect titleRect = [title textRectForBounds:title.bounds limitedToNumberOfLines:999];
CGRect tr = title.frame;
tr.size.height = titleRect.size.height;
title.frame = tr;
}
我希望能够在不同 View 中的 UILabel 上调用 setPageTitle 方法。我该怎么做呢?我在哪里放置此代码以使其工作?我只想将它放在 1 个文件中并让它在不同的 View 中工作。谢谢。
最佳答案
我建议将其作为 UIView 类的一个类别。
UIView+PageTitle.h
@interface UIView (PageTitle)
- (void)setPageTitle:(UILabel *)title withText:(NSString *)text;
@end
UIView+PageTitle.m
#import "UIView+PageTitle.h"
@implementation UIView (PageTitle)
- (void)setPageTitle:(UILabel *)title withText:(NSString *)text {
// your implementation
}
@end
关于iOS 5 SDK 创建随处可用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9218298/