gpt4 book ai didi

ios - iOS 中类似 "styles"的任何内容

转载 作者:行者123 更新时间:2023-11-28 20:12:37 24 4
gpt4 key购买 nike

对 iOS 有点陌生,但学起来很快。

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

帮助!

最佳答案

DB5

一种选择是使用 DB5 ,由 Vesper 发明的系统,用于将字体、颜色等存储在 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;

关于ios - iOS 中类似 "styles"的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19576872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com