如何更改标签中特定文本的属性
How to change properties of particular text in a label
我有一个标签,我以编程方式设置标签的文本。我想将其中一个单词设置为粗体,其余的设置为正常。但是,我无法控制文本的属性。例如,我想要这个 "This is an example" 但我只能实现这个 "This is an example".
看看标签的attributedText
属性。它允许您使用 NSAttributedString
分配样式文本。解释如何构建 NSAttributedString
超出了 SO 答案的范围,但您应该能够在 Xcode 帮助系统和在线中找到充足的信息。
让我给你看一个关于 attributedText 的演示。
NSDictionary*subStrAttribute1 = @{
NSForegroundColorAttributeName: [UIColor redColor],
NSStrikethroughStyleAttributeName:@2
};
NSDictionary *subStrAttribute2 =@{
NSForegroundColorAttributeName: [UIColor greenColor]
};
NSString *strDisplayText3 =@"Red and Green";
NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc] initWithString:strDisplayText3];
[attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0,3)];
[attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8,5)];
self.lblInfo3.attributedText= attributedText3;
试试这个:
NSString *text = @"This is an example";
NSString *textBold = @"example";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString beginEditing];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:20.0f]
range:[text rangeOfString:textBold]];
[attributedString endEditing];
[labelObj setAttributedText:attributedString];
由于ios6uilabel支持属性字符串,所以可以使用。
对于您的特定情况,下面的代码将起作用-
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"This is an example"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(11, 7)];
label.attributedText = string;
我有一个标签,我以编程方式设置标签的文本。我想将其中一个单词设置为粗体,其余的设置为正常。但是,我无法控制文本的属性。例如,我想要这个 "This is an example" 但我只能实现这个 "This is an example".
看看标签的attributedText
属性。它允许您使用 NSAttributedString
分配样式文本。解释如何构建 NSAttributedString
超出了 SO 答案的范围,但您应该能够在 Xcode 帮助系统和在线中找到充足的信息。
让我给你看一个关于 attributedText 的演示。
NSDictionary*subStrAttribute1 = @{
NSForegroundColorAttributeName: [UIColor redColor],
NSStrikethroughStyleAttributeName:@2
};
NSDictionary *subStrAttribute2 =@{
NSForegroundColorAttributeName: [UIColor greenColor]
};
NSString *strDisplayText3 =@"Red and Green";
NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc] initWithString:strDisplayText3];
[attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0,3)];
[attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8,5)];
self.lblInfo3.attributedText= attributedText3;
试试这个:
NSString *text = @"This is an example";
NSString *textBold = @"example";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString beginEditing];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:20.0f]
range:[text rangeOfString:textBold]];
[attributedString endEditing];
[labelObj setAttributedText:attributedString];
由于ios6uilabel支持属性字符串,所以可以使用。 对于您的特定情况,下面的代码将起作用-
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"This is an example"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(11, 7)];
label.attributedText = string;