构造和显示具有样式的Text

1.问题:

你希望能够在UI 组建中直接能够显示富文本,而不是针对每一种格式创建一个单独的UI组件。例如,你想要在一个UILabel 中显示一个句子,该句子中只有一个单词是粗体。

2.富文本是很好的东西!许多程序员需要在UI 组建中的一行文本内显示各种样式的字符串。

构造属性字符串的最好方法就是使用NSMutableAttributedString 类的initWithString:方法,同时传递一个NSString 到这个方法中。这将会创建一个没有任何属性的属性字符串。然后,通过NSMutableAttributedString 类的 setAttributes:range:方法来给字符串中的不同部分设置属性。这个方法有两个参数:

    setAttributes      (attribute  属性)

     是一个字典,字典左右的key 都是字符属性,每个key 的值依赖于key 本身。下面是字典中可以设置的一些重要key:

    NSFontAttributeName:这个key 的值是UIFont 的一个实例,用来定义指定字符串范围的字体。

    NSForegroundColorAttributeName:这个key 的值是UIColor 类型,用来定义指定字符串范围的颜色。

    NSBackgroundColorAttributeName:这个key 的值是UIColor 类型,用来定义指定字符串范围的背景颜色。

    NSShadowAttributeName:这个key 的值必须是NSShadow 的一个实例,用来定义指定字符串范围的阴影。

   range
    是NSRange 类型,用来指定属性应用在字符串的起点和长度。

(注意:要想查看上面这个方法可以传递的所有不同key,可以浏览苹果在线文档关于NSMutableAttributedstring 类介绍。)

3.例子:

“iOS”属性字典可以通过下面的代码来构造:

NSDictionary *attributesForFirstWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSBackgroundColorAttributeName : [UIColor blackColor]
};

“SDK”则使用下面的属性:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor darkGrayColor];
shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);
NSDictionary *attributesForSecondWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSBackgroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName : shadow
};

通过下面的代码,不仅创建了label,还设置好了文本属性:

- (NSAttributedString *) attributedText{
NSString *string = @"iOS SDK";
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]
initWithString:string];
NSDictionary *attributesForFirstWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSBackgroundColorAttributeName : [UIColor blackColor]
};
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor darkGrayColor];
shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);
NSDictionary *attributesForSecondWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSBackgroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName : shadow
};
/* Find the string "iOS" in the whole string and sets its attribute */
[result setAttributes:attributesForFirstWord
range:[string rangeOfString:@"iOS"]];
/* Do the same thing for the string "SDK" */
[result setAttributes:attributesForSecondWord
range:[string rangeOfString:@"SDK"]];
return [[NSAttributedString alloc] initWithAttributedString:result];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.label = [[UILabel alloc] init];
self.label.backgroundColor = [UIColor clearColor];
self.label.attributedText = [self attributedText];
[self.label sizeToFit];
self.label.center = self.view.center;
[self.view addSubview:self.label];
}
原文地址:https://www.cnblogs.com/safiri/p/4033326.html