iOS 5 SDK 创建随处可用的方法

标签 ios iphone methods ios5

如何制作跨多个 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/

相关文章:

Java ArrayDeque 删除和轮询方法错误

ios - 如何从 SecCertificateRef 和 PrivateKey 获取 SecIdentityRef?

iphone - 每点击一次按钮,将文本框中的数字减一

iphone - 续订过期的配置文件时出现问题

iphone - 如何在 objective-c 中获取该月的第 n 个星期日日期?

c# - 使用 C# 访问另一个域中的一个域类文件

ios - 如何设置Info.Plist以使AWSSNS.defaultSNS工作

iphone - 核心数据的单例线程安全吗?

javascript - SignInWithEmailAndPassword (Ionic/Vue/Firebase) 无法解决 iOS 中的 promise

ruby - 用别名包装继承的方法