0

对iOS有点新,但学习很快。

我的应用程序中有许多标签和 UIText 字段。在真正的 iPad 上查看后,我决定将一系列字段上的文本从任何内容更改为 Helv Neu condensed Bold size 24。我认为必须有更好的方法,我可以设置某种类型的样式x 个字段,然后设置样式,但我找不到类似的东西。

帮助!

4

3 回答 3

3

DB5

一种选择是使用由 Vesper 发明的系统DB5将字体、颜色等存储在 plist 中。然后,当您想更改它们时,您可以在一个地方永久更改它们。您甚至可以让您的应用程序从服务器下载新的 Plist,从而允许您远程修改应用程序的外观。

属性字符串字典

另一种选择是使用常量文件来定义属性字符串字典。

基本示例

+ (NSDictionary *) biggerLetterSpacingText
{
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithFloat:1.3] forKey:NSKernAttributeName];
    return dictionary;
}

超越基本的例子

+ (NSDictionary *) linkAttributes
{
    static NSMutableDictionary *dictionary;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dictionary = [NSMutableDictionary dictionary];

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

        [dictionary setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
    });

    UIColor *tintColor = [[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] window] tintColor];

    UIColor *colorToUse;

    if (CGColorEqualToColor(tintColor.CGColor, [UIColor blueColor].CGColor)) {
        // links are enabled
        colorToUse = [UIColor blueColor];
    } else {
        // grey them out
        colorToUse = tintColor;
    }

    [dictionary setObject:colorToUse forKey:(NSString *)kCTForegroundColorAttributeName];
    return dictionary;
}

笔记

此特定代码可用于在 iOS 7 上为超链接着色。如果有帮助,请随意使用更简单的代码。

顶部的dispatch_once代码确保始终使用一个字典(因此您不会在每次调用此方法时都分配一个新字典。

底部的代码根据当前应用程序的色调颜色调整色调颜色。因此,如果弹出 UIAlertView 或 UIActionSheet,所有链接都可以从蓝色变为灰色。

示例代码

要使用它(例如在标签上):

NSString *text = @"Hello, world!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:[MyConstants linkAttributes] range:NSMakeRange(0, [text length])];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.attributedText = attributedString;
于 2013-10-24T22:14:31.627 回答
0

你应该使用一个类别。这是一个例子:

@interface UILabel (FontName)

- (void)setFontName:(NSString *)name;

@end


@implementation UILabel (FontName)

- (void)setFontName:(NSString *)name
{

    self.font = [UIFont fontWithName:name size:self.font.pointSize];
}

@end
于 2013-10-24T21:26:31.390 回答
0

我意识到这是一个古老的问题,但这里有一些其他的框架建议,允许您在应用程序中使用可重用样式:

开源:

封闭源(但免费):

  • nativeCSS - http://nativecss.com/
    • 使用具有标准 CSS/HTML 属性名称的高级样式表,以及一些非标准添加
    • 支持多种平台
于 2014-06-04T20:56:32.430 回答